-2

I have an api that returns a nested object of this form:

obj: {
    nestedobj1: {
        name: 'a',
        prop2: 'b',
        prop3: 'c',
        prop4: 'd'
    },
    nestedobj2: {
        name: 'a',
        prop2: 'b',
        prop3: 'c',
        prop4: 'd'
    },
    nestedobj3: {
        name: null,
        prop2: null,
        prop3: null,
        prop4: null
    },
    nestedobj4: {
        name: null,
        prop2: null,
        prop3: null,
        prop4: null
    },
// and so on up to 15 nested objects whose internal properties are null
}

These nested objects are supposed to hold a row of table data. When the user enters the details for the fields, the inputs need to be saved in one of the nested object for which the name value is null. How do I achieve this in javascript?

user
  • 749
  • 1
  • 7
  • 23
  • please add your try. – Nina Scholz Jun 28 '19 at 08:11
  • 2
    This is in object with keys, not an ordered array. Does **first** object matter or just **an** object that's empty? Given the names of the keys, maybe this *should* be an array. – Mark Jun 28 '19 at 08:12
  • 2
    Sounds like an X/Y problem - how to not save null values into a user generated array – mplungjan Jun 28 '19 at 08:13
  • Re-reading I imagine you have a fixed object and you need to find the first empty slot. If you did NOT have a fixed object but just an array, you could just push the new set to the end – mplungjan Jun 28 '19 at 08:20
  • Yes it is a fixed array which can hold at most 15 objects. I need to find the first object that has a null name and write to it – user Jun 28 '19 at 08:22
  • See https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties – str Jun 28 '19 at 08:36
  • 2
    There is no concept of "first" or order in an object, you can iterate over the keys to find *a* property with null values, but two different implementations might find different properties given the same starting object. – RobG Jun 28 '19 at 09:01

1 Answers1

1

If I understand correctly, you want to extract the first a key/value entry from obj where the value object itself contains any null values.

One way to achieve that would be to:

  1. extract the key/value entries of obj via Object.entries() and then,
  2. filter the entries array for any where the entry value object contains one or more null values (this can be done using Object.values() and .some() - see snippet below),
  3. reconstruct any entries that pass the previous filtering step to original object form
  4. return the first matching result, if any

This could be expressed in code as:

const obj={nestedobj1:{name:'a',prop2:'b',prop3:'c',prop4:'d'},nestedobj2:{name:'a',prop2:'b',prop3:'c',prop4:'d'},nestedobj3:{name:null,prop2:null,prop3:null,prop4:null},nestedobj4:{name:null,prop2:null,prop3:null,prop4:null}};

/* Pluck first value from result array, if any */
const [ firstObjectWithNull ] = Object.entries(obj)
/* Filter/find any key/value entries from state object where object values
have any nulls */
.filter(entry => {

  const [key, object] = entry;
  const objectValues = Object.values(object);

  /* With "some()", find the first entry where the values of the object 
  have a null */
  return objectValues.some(value => value === null);
})
.map(entry => {
  
  /* Reconstuct any fiiltered entries to key/value objects */
  const [key, object] = entry;
  return { [key] : object };
});

console.log(firstObjectWithNull);
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
  • 1
    `Array#find` would be the more idiomatic solution than `filter`/`map`/`[0]`. But then again, [object properties are not necessarily ordered](https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties). – str Jun 28 '19 at 08:30