我有一个对象数组:
var items = [
{
"id":"sugar",
"type": 'eatables'
},
{
"id":"petrol",
"type": 'utility'
},
{
"id":"apple",
"type": 'fruits'
},
{
"id":"mango",
"type": 'fruits'
},
{
"id":"book",
"type": 'education'
}
];
Run Code Online (Sandbox Code Playgroud)
现在我有另一个订单数组,我想借助它对items数组进行排序:
var orders = [
{
"id":"sugar",
"order":5
},
{
"id":"book",
"order":1
}
];
Run Code Online (Sandbox Code Playgroud)
到目前为止,我在逻辑中所尝试的是我放置了太多循环,以至于完全造成了混乱。
任何人都可以为此提供简短且优化的逻辑吗?
我有一个日期范围
日期范围示例:
const startDate = "2022-06-02";
const endDate = "2022-06-20";
Run Code Online (Sandbox Code Playgroud)
我想获取在 startDate 和 endDate 之间提供的日期数组中出现的日期
天数数组示例:
["tuesday", "friday", "saturday"]
Run Code Online (Sandbox Code Playgroud)
预期结果是:
2022-06-03
2022-06-04
2022-06-07
2022-06-10
2022-06-11
2022-06-14
2022-06-17
2022-06-18
Run Code Online (Sandbox Code Playgroud)
谁能帮我解决这个逻辑?
What I tried was so dirty, I put a loop on range of dates, and got the list of all dates, and then i put another loop to get name of day of each date, and then compared each day name in an array of days & pushed that date to …
我有一个非常简单的数组:
var arr = [{id: 1, score: 10}, {id: 1, score: 10}, {id: 3, score: 20}, {id: 4, score: 5}];
Run Code Online (Sandbox Code Playgroud)
我想删除那些只出现一次的对象,例如:
{id: 3, score: 20}
{id: 4, score: 5}
Run Code Online (Sandbox Code Playgroud)
所以最终的输出应该是:
[{id: 1, score: 10}, {id: 1, score: 10}]
Run Code Online (Sandbox Code Playgroud)
到目前为止我尝试过的是:
const result = [];
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i].id === arr[j].id && arr[i].score === arr[j].score) {
result.push({ id: arr[i].id, score: arr[i].score }) …Run Code Online (Sandbox Code Playgroud)