Accessing nested keys in object and compare them to other object keys, but the 2nd one is an array of objects

Related searches

So I got this issue trying to compare and access data in nested objects and compare tham to an array with objects (this is not nested).

To simulate it looks something like this:

const members = [
      {
        name: 'Angelica',
        age: '25',
        sex: 'female',
        phone: '123456789',
      },
      {
        name: 'Dominic',
        age: '30',
        sex: 'male',
        phone: '987654321',
      },
    ];

    const updates = {
      123456789: {
        date: '12.02-2019',
        warnings: {},
        errors: {}
      },
      987654321: {
        date: '20.01-2020',
        warnings: {
          somethinghere: {
            warning1: 'warning',
            allok: 'yes',
          }
        },
        errors: {},
      }
    }

So first I need to check if phone in members is equal to the key in object, then I need to find out if there is any warnings (warnings not empty). I've tried to loop through members with forEach as members.forEach((member) => { if (Object.keys(updates) === member.name) } But it does not seem to work. The info like warning in updates is not declared initially, but will appear in the state after certain changes.

const members = [
    {
      name: 'Angelica',
      age: '25',
      sex: 'female',
      phone: '123456789',
    },
    {
      name: 'Dominic',
      age: '30',
      sex: 'male',
      phone: '987654321',
    },
  ];

  const updates = {
    123456789: {
      date: '12.02-2019',
      warnings: {},
      errors: {}
    },
    987654321: {
      date: '20.01-2020',
      warnings: {
        somethinghere: {
          warning1: 'warning',
          allok: 'yes',
        }
      },
      errors: {},
    }
  }

  members.forEach(member => {
      Object.entries(updates).forEach(([key, value]) => {
          if(key === member.phone && JSON.stringify(value.warnings) === '{}'){
                console.log(key)
          }
      })
  })

Accessing Nested Objects in JavaScript, tldr; safely access nested objects in JavaScript in a super cool way. One of those things is the confrontation with this error when you try to access This is because we're trying to access name key from an object that does not exist. I picked this style from stackoverflow a while back and it is pretty catchy� 2 Accessing nested keys in object and compare them to other object keys, but the 2nd one is an array of objects Jan 30 2 Set minPointLength for one of two series in highchart May 16 '19 1 Disable pointer-event when hovering router button if disabled May 31 '19

You could use map and Object.keys methods.

const members = [ { name: 'Angelica', age: '25', sex: 'female', phone: '123456789', }, { name: 'Dominic', age: '30', sex: 'male', phone: '987654321', }, ];
const updates = { 123456789: { date: '12.02-2019', warnings: {}, errors: {} }, 987654321: { date: '20.01-2020', warnings: { somethinghere: { warning1: 'warning', allok: 'yes', } }, errors: {}, } }


var result = members.map(({phone}) => ({phone, hasWarnings: Object.keys(updates[phone].warnings).length > 0}) );
console.log(result);

Objects, The second one has the name "age" and the value 30 . The const fixes the value of user , but not its contents. There's another way to make constant object properties, we'll cover it later in And then we use it to access the property. A notable feature of objects in JavaScript, compared to many other� Array; Plain objects also support similar methods, but the syntax is a bit different. Object.keys, values, entries. For plain objects, the following methods are available: Object.keys(obj) – returns an array of keys. Object.values(obj) – returns an array of values. Object.entries(obj) – returns an array of [key, value] pairs.

Object.keys(updates) is an array so you should use includes:

if (Object.keys(updates).includes(member.phone) )

Data Structures: Objects and Arrays :: Eloquent JavaScript, Many types of information require more than one atom, though. In the second, we access the property named max in the Math object (which is a They grasp values, but other bindings and properties might be holding onto those same values. When you compare objects with JavaScript's == operator, it compares by� If you want to do them in a particular order, you have to get an array of them, sort it in the order you want, and then loop through that array. (In an ES5-enabled environment, you can get an array of the keys of an object from Object.keys(yourObject). But you'd need a shim for older browsers.)

You could just do const update = updates[member.phone]; to see if an update is available, this will be undefined if no such phone exists in updates key set.

Then const warnings = Object.keys(update.warnings); will give you list of warning keys.

const members = [{
  name: 'Angelica',
  age: '25',
  sex: 'female',
  phone: '123456789',
}, {
  name: 'Dominic',
  age: '30',
  sex: 'male',
  phone: '987654321',
}, ];
const updates = {
  123456789: {
    date: '12.02-2019',
    warnings: {},
    errors: {}
  },
  987654321: {
    date: '20.01-2020',
    warnings: {
      somethinghere: {
        warning1: 'warning',
        allok: 'yes',
      }
    },
    errors: {},
  }
}

members.forEach((member) => {
  const update = updates[member.phone];
  if (update) {
    const warnings = Object.keys(update.warnings);
    console.log(warnings);
    if (warnings.length === 0) {
      console.log(`no warnings for member ${member.name}'s update`);
    }
  } else {
    console.log(`no updates for member ${member.name}`);
  }
});

How to simplify your codebase with map(), reduce(), and filter() in , Well, this is the only one not about map/reduce/filter, but it's so compact that it was hard not The filter() method creates a new array with all elements that pass the test Then the first value of the nested will be used as the initial acc value. Create an object that contains the frequency of the specified key. In the above example we store an array of keys in the keys const. We then can easily access the amount of properties on the object by checking the length of the keys array. Getting the values with: Object.values() The complementary function of Object.keys() is Object.values(). This function takes an object as an argument and returns an array of

4 different techniques for copying objects in JavaScript, We call the copy shallow because the properties in the target object can still source object but hardly care about references to any nested properties, e.g. be a need to change a given object on a deeper level (nested objects/arrays). it directly alters (“mutates”) the target object whereas the second one� A better approach is to create a new object that works a lot like nil but supports this behavior. Make this new object the default return of your hashes. And then it will just work. Alternately you can create a simple "nested lookup" function that you pass the hash and the keys to, which traverses the hashes in order, breaking out when it can.

I believe that in many other cases, indexing dictionary keys by position can be avoided. Although dictionaries are ordered in Python 3.7, relying on that is not pretty. The code above only works because the contents of some_list had been recently produced from the contents of d .

Now the relationship is explicit, and you can iterate over the array more simply and safely than iterating over the keys. (Safely because, if a property is added to Object.prototype, it will show up as a key.)

Comments
  • You want to check phone only or all members attributes?
  • @ZivBen-Or Phone only before I do something further.
  • O.K, so see my answer below :-)
  • Thank you so much ♥
  • Is it possible to also check if value.warnings object as same amount of keys as errors? So if there is 2 keys in warnings its true if also errors has 2 keys
  • Try this: if(Object.keys(value.warnings).length == Object.keys(value.errors).length)