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
4 Answers
Reset to default 6Create 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']));