1

I have a nested object having structure like this:

let obj = {
  id1: {
    key1: value1,
    files: {
      fileid1: {},
      fileid2: {}
    }
  },
  id2: {
    key1: value1,
    files: {
      fileid3: {},
      fileid4: {}
    }
  }
}

I have an existing file id , I need to find that file id from this object and update the data of the object. let say I have file id that is equal to fileid3 How can I do this ?

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
kindacoder
  • 143
  • 1
  • 9

4 Answers4

1

let obj = {
    id1: {
        key1: "value1",
        files: {
            fileid1: {},
            fileid2: {}
        }
    },
    id2: {
        key1: "value1",
        files: {
            fileid3: {},
            fileid4: {}
        }
    }
}

function changeObj(obj, field, value) {
    Object.keys(obj).forEach(key => {

        if (obj[key].files.hasOwnProperty(field))
            obj[key].files[field] = value;

    });
    return obj;
}

console.log(changeObj(obj, "fileid3", "new Value"));
0

You can use .find() on the Object.values() of your object. For each value (ie object), you can check whether the .files object has the property of the fileid you pass into your function. If it does you can return true by using .hasOwnProperty(). You can then use .files[id] on the return value of .find() to get your object:

const obj = { id1: { key1: "value1", files: { fileid1: {}, fileid2: {} } }, id2: { key1: "value1", files: { fileid3: {}, fileid4: {} } } };

const findObjectByFile = (obj, id) => 
  (Object.values(obj).find(({files}) => files.hasOwnProperty(id)) || {files: {}}).files[id];
 
const file_obj = findObjectByFile(obj, "fileid");
file_obj.foo = "bar";
console.log(obj);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

Just use Object.values to iterate through the first level of the object and check if the files object contains the property you are looking for using the in operator.

let obj = {
  id1: {
    key1: "value1",
    files: {
      fileid1: {},
      fileid2: {}
    }
  },
  id2: {
    key1: "value1",
    files: {
      fileid3: {},
      fileid4: {}
    }
  }
};

editFileID("fileid3", "new value");
console.log(obj.id2.files.fileid3);

function editFileID(fileID, newValue) {
  Object.values(obj).some(function(obj) {
    if (fileID in obj.files) {
      obj.files[fileID] = newValue;
      return true;
    }
  });
}
nick zoum
  • 7,216
  • 7
  • 36
  • 80
0

Here is the example JSON object:

const myJSONExample = {
  id1: {
    key1: "value1",
    files: {
      fileid1: "2",
      fileid2: "3"
    }
  },
  id2: {
    key1: "value1",
    files: {
      fileid3: "4",
      fileid4: "5"
    }
  }
}

Now iterate over the object, find your specific key and update its value. If a key is a nested object, we use recursion.

const iterate = (obj) => {
    Object.keys(obj).forEach(key => {

    if(key == 'fileid3'){
      obj[key] = 'asd';
    }
    if (typeof obj[key] === 'object') {
            iterate(obj[key])
        }
    })

}

iterate(myJSONExample);
console.log(myJSONExample);

OUTPUT:

{ 
  id1: 
     { 
       key1: 'value1', 
       files: 
            { fileid1: '2', fileid2: '3' }
     },  
  id2: 
     { 
       key1: 'value1', 
       files: 
           { fileid3: 'asd', fileid4: '5'} 
     } 
 }

fileid3 value has been updated

reference link: Iterate through Nested JavaScript Objects

SU7
  • 1,586
  • 1
  • 18
  • 26