15 Aug 2019

Search in a Array of Objects javascript

This are solutions to search data on arrays

const obj1 = {
    name = 'johan',
    age= '29',
}
const obj2 = {
    name = 'peter',
    age= '20',
}
const arrayOfObjects = [obj1, obj2];

const index = arrayOfObjects.findIndex((obj) => obj.name === 'johan')

console.log(arrayOfObjects[index].name)
// output : 'johan'
let data = {
  12: {
    name: 'johan',
    age: '29',
  },
  40: {
    name: 'sebas',
    age: '15',
  },
};

let updateRow = {
  name: 'ronca',
  age: '56',
};

const index = '40';

if (Object.keys(data).includes(index)) {
  Object.keys(data[index]).map(
    (key) =>
      data[index][key] != updateRow[key] && (data[index][key] = updateRow[key])
  );
}

console.log(data);
let data = {
  12: {
    name: 'johan',
    age: '29',
  },
  40: {
    name: 'sebas',
    age: '15',
  },
};
let data2 = {};
const index = '20';


console.log(!Object.keys(data2).includes(index));
© 2019 Jsuarez.Dev