【问题标题】:how to find if particular objects exists in array? [duplicate]如何查找特定对象是否存在于数组中? [复制]
【发布时间】:2021-10-05 05:02:58
【问题描述】:

我有一个数组

let bingo = [{"Device": "fan", "Manafacturer": "Havells"}, {"Device": "Ceiling", "Manafacturer": "bajaj"}]

如何检查特定对象是否存在 在这种情况下,就这样吧

let obj ={"Device": "Ceiling", "Manafacturer": "bajaj"}
//should return true or {"Device": "Ceiling", "Manafacturer": "bajaj"}

 obj ={"Device": "light", "Manafacturer": "bajaj"}
//should return false or undefined

【问题讨论】:

  • 错字:制造商
  • 所有这些都可以在 MDN 文档中找到。你应该好好利用它。
  • @RobertHarvey 如果我错了请纠正我,但我认为 .includes 除非引用相同,否则不会起作用。

标签: javascript arrays


【解决方案1】:

JavaScript 数组有一个名为 find 的函数,它返回数组中所有符合条件的元素:

let bingo = [{
  "Device": "fan",
  "Manafacturer": "Havells"
}, {
  "Device": "Ceiling",
  "Manafacturer": "bajaj"
}];
let obj = {
  "Device": "Ceiling",
  "Manafacturer": "bajaj"
};

let result = bingo.find(bingoObj => bingoObj.Manafacturer === obj.Manafacturer);

console.log(result);
// logs {"Device": "Ceiling", "Manafacturer": "bajaj"}

请参阅https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/find 以获取有关查找功能的更多参考

【讨论】:

  • 这不是被骗的可能性基本为零。
【解决方案2】:

您可以将其转换为字符串,然后使用indexOf() 来检查它是否包含在内。这对于嵌套的复杂对象也很有用。正如 Lain 所评论的那样,密钥必须按相同的顺序排列,否则会失败:

let bingo = [{"Device": "fan", "Manafacturer": "Havells"}, {"Device": "Ceiling", "Manafacturer": "bajaj"}];

let check1 ={"Device": "Ceiling", "Manafacturer": "bajaj"}
//should return true or {"Device": "Ceiling", "Manafacturer": "bajaj"}

let check2 ={"Device": "light", "Manafacturer": "bajaj"}
//should return false or undefined

function checkObjectExists(main, check) {
  return JSON.stringify(main).indexOf(JSON.stringify(check)) >= 0;
}

console.log( checkObjectExists(bingo, check1) );   //true

console.log( checkObjectExists(bingo, check2) );  //false

【讨论】:

  • 这不是被骗的可能性基本为零。
  • 只需更改obj 中键的顺序即可使其失败。
【解决方案3】:

您可以传入一个对象,在其中检查条件的每个键值对;或者您传入一个函数,您可以在其中访问每个项目的值并检查它们以进行比较。

如果不进行严格检查,每个项目只会被序列化为 JSON 并与 JSON 标准值进行比较。这是最不理想的检查方式。

const assertTrue  = (v) => { if (v !== true)  throw new Error('value is not true');  };
const assertFalse = (v) => { if (v !== false) throw new Error('value is not false'); };

const arrayContains = (arr, criteria, strict = true) => arr.find(item =>
  typeof criteria === 'function'
    ? criteria(item)
    : strict
      ? Object.entries(criteria).every(([k, v]) => (item[k] === v))
      : JSON.stringify(item) === JSON.stringify(criteria)
    ) != null

const bingo = [
  { "Device": "fan"     , "Manufacturer": "Havells" },
  { "Device": "Ceiling" , "Manufacturer": "bajaj"   }
];

let found = arrayContains(bingo, { "Device": "Ceiling", "Manufacturer": "bajaj" });
assertTrue(found);

found = arrayContains(bingo, ({ Device, Manufacturer }) =>
  Device === 'Ceiling' && Manufacturer === 'bajaj');
assertTrue(found);

found = arrayContains(bingo, { "Device": "light", "Manufacturer": "bajaj" });
assertFalse(found);

console.log('Testing complete...');
.as-console-wrapper { top: 0; max-height: 100% !important; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 2020-04-02
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2019-01-12
    相关资源
    最近更新 更多