怎么判断每项orders里的id是否重复

怎么判断每项orders里的id是否重复

怎么判断每项orders里的id是否重复

[{

id:'1',

name:1,

orders:[

{

id:'2',

name:1,

},

{

id:'2',

name:1,

}

]

},

{

id:'1',

name:1,

orders:[

{

id:'2',

name:1,

}

]

}]


回答:

let list = [{

id:'1',

name:1,

orders:[

{

id:'2',

name:1,

},

{

id:'2',

name:1,

}

]

},

{

id:'1',

name:1,

orders:[

{

id:'2',

name:1,

}

]

}];

list.every(item=>{

let ids =item.orders.map(order=>order.id)

return new Set(ids).size==ids.length

});

结果 false


回答:

const data = [

{

id: "1",

name: 1,

orders: [

{

id: "2",

name: 1,

},

{

id: "2",

name: 1,

},

],

},

{

id: "1",

name: 1,

orders: [

{

id: "2",

name: 1,

},

],

},

];

const result = [];

// 数组项是否重复

const isRepeat = (arr) => {

const hash = {};

const result = arr.some((item) => {

if (hash[item]) {

return true;

} else {

hash[item] = true;

return false;

}

});

return result;

};

data.forEach((item, index) => {

result.push(isRepeat(item.orders));

});

// [ true, false ] true表示有重复项,false表示没有重复项

console.log(result);

以上是 怎么判断每项orders里的id是否重复 的全部内容, 来源链接: utcz.com/p/935871.html

回到顶部