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 ?

2
  • So you just want to get the object corresponding to fileid3? Commented Dec 18, 2019 at 8:18
  • @nickzoum yes, i need to update the key value pairs for fileid3 object Commented Dec 18, 2019 at 8:19

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.