# 请问大家关于这个数组对象的算法，谢谢大家？

``````let arr1 = [
{
"enter_warehouse_id": "6",
"goods_sn": "9110011603",
},
{
"enter_warehouse_id": "21",
"goods_sn": "8011115748541",
},
{
"enter_warehouse_id": "21",
"goods_sn": "8033129700",
},
{
"enter_warehouse_id": "21",
"goods_sn": "8011400300",
},
{

"enter_warehouse_id": "21",
"goods_sn": "8011400200",
}
]``````

5 个回答
✓ 已被采纳
``arr1.every((item, index, arr) => item.enter_warehouse_id === arr[0].enter_warehouse_id)``
``````function isUnique(arr) {
const uniqueIds = arr.reduce((set, obj) => {
return set;
}, new Set());
return uniqueIds.size === 1;
}

const arr1 = [
{
enter_warehouse_id: "21",
goods_sn: "8011115748541",
},
{
enter_warehouse_id: "21",
goods_sn: "8033129700",
},
{
enter_warehouse_id: "21",
goods_sn: "8011400300",
},
{
enter_warehouse_id: "21",
goods_sn: "8011400200",
},
];

console.log(isUnique(arr1)) ``````
``````const checkIsEnterWarehouseIdUnique = (arr) => {
const len = arr.length
// TODO length 为0时的返回值，根据自身业务特点赋值
if (len === 0) {
return
} else if (len === 1){
return true
}
const firstItemId = arr[0].enter_warehouse_id
for (let i = 1; i < len; i++){
if (arr[i].enter_warehouse_id !== firstItemId) {
return false
}
}
return true
}``````

``new Set(arr1.map(item=>item.enter_warehouse_id)).size > 1``

arr1.every((item, index, arr) => item.enter_warehouse_id === arr[0].enter_warehouse_id)

arr1.find((item, index, array) => item.enter_warehouse_id !== array[0].enter_warehouse_id)

###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进