检查数组是否具有具有特定属性的对象

Check if an array has an object with a certain property

提问人:Alex 提问时间:11/17/2023 更新时间:11/21/2023 访问量:75

问:

我有一个对象数组:

const arr = [
{ puzzle: [1,3,4,8,0,2,7,6,5], children: [] }, 
{ puzzle: [1,2,3,4,5,6,7,8] }
]

我有一个对象:

const element = { puzzle: [1,3,4,8,0,2,7,6,5], children: ["1"]}

我试图查看 element.puzzle 是否是数组中任何对象的属性值 - 在这个特定示例中,但我尝试的一些方法给出了错误的结果,所以显然我错了......element.puzzle === arr[0].puzzle

array.some() 方法给出错误的结果

arr.some(item => item.puzzle === element.puzzle) // gives false
arr.find(item=>item.puzzle === element.puzzle) //gives undefined
JavaScript 数组

评论

0赞 code 11/17/2023
@ControlAltDel JavaScript 中不存在该函数。

答:

2赞 Mr. Polywhirl 11/17/2023 #1

对于数组中的每个项目,检查是否至少有一个拼图具有相同的长度,并且与元素拼图中的每个索引/值匹配。

const
  arr = [
    { puzzle: [1,3,4,8,0,2,7,6,5], children: [] }, 
    { puzzle: [1,2,3,4,5,6,7,8] }
  ],
  element = { puzzle: [1,3,4,8,0,2,7,6,5], children: ["1"]}

const arrHasPuzzle = arr.some(({ puzzle }) =>
  puzzle.length === element.puzzle.length &&
  puzzle.every((e, i) => e === element.puzzle[i]));

console.log(arrHasPuzzle); // true

0赞 thgaskell 11/17/2023 #2

问题在于这两个谜题都是数组的不同实例。由于数组是对象,因此在比较相等性时,即使它们包含相同的值,它们也会被视为不同的值。

[1,2,3] === [1,2,3] //false

相反,您必须将两个数组中每个元素的值相互比较,以判断它们是否“等效”。

arr.some(item => item.puzzle.every((e, i) => element.puzzle[i] === e))
arr.find(item => item.puzzle.every((e, i) => element.puzzle[i] === e))
0赞 Carsten Massmann 11/17/2023 #3

Polywhirl 先生提供了一个非常简洁有效的解决方案。但是,每当 和 的元素顺序不同时,它就会返回。以下代码片段显示了如何解决该问题:false.puzzlearr[i]element

const
  arr = [
    { puzzle: [1,3,4,8,0,2,7,6,5], children: [] }, 
    { puzzle: [1,2,3,4,5,6,7,8] }
  ],
  element = { puzzle: [1,4,3,8,0,2,7,6,5], children: ["1"]}

element.pzl=element.puzzle.slice(0).sort();
const arrHasPuzzle = arr.some(({ puzzle }) =>
  puzzle.length === element.pzl.length &&
  puzzle.slice(0).sort().every((e, i) => e === element.pzl[i]));

console.log(arrHasPuzzle); // true

0赞 Selaka Nanayakkara 11/17/2023 #4

另一种不使用内置运算符的解决方案:

const arr = [
  { puzzle: [1, 3, 4, 8, 0, 2, 7, 6, 5], children: ["1"] },
  { puzzle: [1, 2, 3, 4, 5, 6, 7, 8] }
];

const element = { puzzle: [1, 3, 4, 8, 0, 2, 7, 6, 5], children: ["1"] };

function findElement(arr, element) {
  for (let i = 0; i < arr.length; i++) {
    const currentElement = arr[i];
    if (
      currentElement.puzzle &&
      JSON.stringify(currentElement.puzzle) === JSON.stringify(element.puzzle) &&
      JSON.stringify(currentElement.children) === JSON.stringify(element.children)
    ) {
      return i; // Return the index if the element found
    }
  }
  return -1; // Return -1 if the element is not found
}

const index = findElement(arr, element);

if (index !== -1) {
  console.log(`Element found at index ${index}`);
  console.log(JSON.stringify(arr[index]))
} else {
  console.log('Element not found');
}

0赞 PeterKA 11/21/2023 #5

您可以使用 和 如下所示:Array#some()Array#every()

const m arr.some(
  item => item.puzzle.every((e,i) => e === element.puzzle[i])
);

演示

const arr = [
  { puzzle: [1,3,4,8,0,2,7,6,5], children: [] }, 
  { puzzle: [1,2,3,4,5,6,7,8] }
];

const element = { puzzle: [1,3,4,8,0,2,7,6,5], children: ["1"]};

const m = arr.some(
  item => item.puzzle.every((e,i) => e === element.puzzle[i])
);

console.log( m );