kindacoder
kindacoder

Reputation: 163

How to modify the property of nested object using key detail

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 ?

Upvotes: 1

Views: 46

Answers (4)

Sachintha Nayanajith
Sachintha Nayanajith

Reputation: 721

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"));

Upvotes: 1

SU7
SU7

Reputation: 1756

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

Upvotes: 0

Nick Parsons
Nick Parsons

Reputation: 50759

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);

Upvotes: 0

nick zoum
nick zoum

Reputation: 7305

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;
    }
  });
}

Upvotes: 0

Related Questions