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
7
  • 1
    Array find Commented Jul 29, 2021 at 14:16
  • or Array.Includes. Commented Jul 29, 2021 at 14:17
  • Typo: Manufacturer Commented Jul 29, 2021 at 14:18
  • All of which are readily available in the MDN documentation. You should avail yourself of it. Commented Jul 29, 2021 at 14:18
  • @RobertHarvey correct me if I'm wrong, but I don't think .includes will work unless the reference is identical. Commented Jul 29, 2021 at 14:19

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

The chance of this not being a dupe is basically zero.
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

2 Comments

The chance of this not being a dupe is basically zero.
One just has to change the order of keys in obj to make it fail.
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; }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.