1

I have this JSON object:

var collection = {
    "123":{
      "name": "Some Name",
      "someArray": [
          "value 0",
          "value 1"
       ]
     },
    "124":{
      "name": "Some Name"
     }, 

I have a method that updates and returns the collection like:

function updateCollection(id, property, value){
return collection;
}

Suppose the method call is like this:

updateCollection(124, someArray, "value 3");

How should I update? What I already wrote is:

function updateCollection(id, property, value){
  if(collection[id].hasOwnProperty(property)){
    collection[id][property].push(value);
  }
  else{
   //need help here 
  }
  return collection;
}

Expected Output after calling method updateCollection(124, someArray, "value 3"); should be:

"124":{ "name": "Some Name", "someArray": [ "value 3", ] }
3
  • collection[id] = [value];? Commented Mar 22, 2019 at 8:07
  • What is the expected output for updateCollection(124, someArray, "value 3");? Commented Mar 22, 2019 at 8:09
  • expected output after the call updateCollection(124, someArray, "value 3"); should be:"124":{ "name": "Some Name", "someArray": [ "value 3", ] }, Commented Mar 22, 2019 at 8:11

4 Answers 4

6

Create a new array with just value and assign it to collection[id][property]:

function updateCollection(id, property, value) {
  collection[id] = collection[id] || {}; // if "id" doesn't exist in collection
   if (collection[id].hasOwnProperty(property) {
      collection[id][property].push(value);
    } else {
      collection[id][property] = [value]
    }
    return collection;
  }
Sign up to request clarification or add additional context in comments.

2 Comments

No, this answer does not handle the case where the collection does not contain the property id. In this case your code will raise an exception. Have a look at the the answers posted by Nina and Prabu which cover this case. I also came to a similar solution here: jsfiddle.net/WittigMarcus/v1xey2s6
@Marcus I'm well aware of that. OP's question doesn't indicate that they are going to send an id which doesn't exist in collection. Answers are based on the assumption that OP has provided a minimal reproducible example. If it is a possibility, OP has to mention that in the question. OP doesn't mention that this function is taking closure over the collection object either, but it's assumed collection object exists. Is it expected to give code snippets that cover edge cases?
2

I would go a step ahead and insert a new object for any id which does not exist and create a new array for property, if necessary.

function updateCollection(id, property, value) {
    collection[id] = collection[id] || {};
    collection[id][property] = collection[id][property] || [];
    collection[id][property].push(value);
    return collection;
}

Comments

1

I have updated you code and it should work better for cases with not existed array or values

var collection = {
    "123":{
      "name": "Some Name",
      "someArray": [
          "value 0",
          "value 1"
       ]
     },
    "124":{
      "name": "Some Name"
     }};
function updateCollection(id, property, value) {
  if (collection[id] && collection[id][property]) {
      collection[id][property].push(value);
    }
    else
    {
    collection[id] = {};
      collection[id][property] = [value]
    }
  return collection;  
  }



  updateCollection(124, "someArray", "value 3");
  updateCollection(125, "someArray", "value 3");
  console.log(collection);

Comments

0

Here your code.

var collection = {
"123":{
  "name": "Some Name",
  "someArray": [
      "value 0",
      "value 1"
   ]
 },
"124":{
  "name": "Some Name"
 }
 };

 function updateCollection(id, property, value){
  var _coll = collection.hasOwnProperty(id) ? collection[id] : {};
  _coll[property] = value;
  collection[id] = _coll;
  return collection;
 }

 console.log(updateCollection(124, "somearray", ['1','2']));

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.