1

I have an array

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

how to check if particular object exists in this case let it be

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

 obj ={"Device": "light", "Manafacturer": "bajaj"}
//should return false or undefined
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
jeena
  • 33
  • 4

3 Answers3

2

JavaScript Arrays have a function called find, which returns all elements of an array, that match the condition:

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"}

see https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/find for further reference on the find function

marschelpoet
  • 170
  • 1
  • 10
1

You can convert it to a string and then use indexOf() to check if it is included. This would also be useful for nested complex objects. Keys must be in the same order or it would fail, as commented by 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
AlexSp3
  • 2,201
  • 2
  • 7
  • 24
0

You could either pass in an object, where you check each key-value pair of the criteria; or you pass in a function, where you access value of each item and check them for comparison.

Without strict checking, each item will just be serialized into JSON and compares with a JSON criteria value. This is the least optimal way to check.

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; }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132