最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding an array to a JSON object if it doesn't exist - Stack Overflow

programmeradmin1浏览0评论

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", ] }

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", ] }
Share Improve this question edited Nov 15, 2019 at 3:12 mav-raj asked Mar 22, 2019 at 8:05 mav-rajmav-raj 7711 gold badge9 silver badges22 bronze badges 3
  • collection[id] = [value];? – jonrsharpe Commented Mar 22, 2019 at 8:07
  • What is the expected output for updateCollection(124, someArray, "value 3");? – adiga 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", ] }, – mav-raj Commented Mar 22, 2019 at 8:11
Add a ment  | 

4 Answers 4

Reset to default 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;
  }

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

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

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

发布评论

评论列表(0)

  1. 暂无评论