I have declared a JSON Object and added some key value pair in that like:
var obj = {};
and added some data into it like:
obj = {
"1":"aa",
"2":"bb"
};
But I want to add more key value pair in the same object, if I add key value pair same above mentioned then it replace the old one. So could any one please tell me how I can append data in the same JSON Object i.e. obj.
This question is related to
javascript
json
Hi I add key and value to each object
let persons = [_x000D_
{_x000D_
name : "John Doe Sr",_x000D_
age: 30_x000D_
},{_x000D_
name: "John Doe Jr",_x000D_
age : 5_x000D_
}_x000D_
]_x000D_
_x000D_
function addKeyValue(obj, key, data){_x000D_
obj[key] = data;_x000D_
}_x000D_
_x000D_
_x000D_
let newinfo = persons.map(function(person) {_x000D_
return addKeyValue(person, 'newKey', 'newValue');_x000D_
});_x000D_
_x000D_
console.log(persons);
_x000D_
you can do try lodash
Example code for json object:
var user = {'user':'barney','age':36};
user["newKey"] = true;
console.log(user);
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="lodash.js"></script>
_x000D_
for json array elements
Example code:
var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 }
];
users.map(i=>{i["newKey"] = true});
console.log(users);
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="lodash.js"></script>
_x000D_
possible duplicate , best way to achieve same as stated below:
function getKey(key) {
return `${key}`;
}
var obj = {key1: "value1", key2: "value2", [getKey('key3')]: "value3"};
Please try following simple operations on a json, insert/update/push:
var movie_json = {
"id": 100,
};
//to insert new key/value to movie_json
movie_json['name'] = 'Harry Potter';
console.log("new key: " + movie_json);
//to update a key/value in movie_json
movie_json['id'] = 101;
console.log("updated key: " +movie_json);
//adding a json array to movie_json and push a new item.
movie_json['movies']=["The Philosopher's Stone"];
movie_json['movies'].push('The Chamber of Secrets');
console.log(movie_json);
You can use dot notation or bracket notation ...
var obj = {};
obj = {
"1": "aa",
"2": "bb"
};
obj.another = "valuehere";
obj["3"] = "cc";
Object assign copies one or more source objects to the target object. So we could use Object.assign
here.
Syntax: Object.assign(target, ...sources)
var obj = {};_x000D_
_x000D_
Object.assign(obj, {"1":"aa", "2":"bb"})_x000D_
_x000D_
console.log(obj)
_x000D_
You can add more key value pair in the same object without replacing old ones in following way:
var obj = {};
obj = {
"1": "aa",
"2": "bb"
};
obj["3"] = "cc";
Below is the code and jsfiddle link to sample demo that will add more key value pairs to the already existed obj on clicking of button:
var obj = {
"1": "aa",
"2": "bb"
};
var noOfItems = Object.keys(obj).length;
$('#btnAddProperty').on('click', function() {
noOfItems++;
obj[noOfItems] = $.trim($('#txtName').val());
console.log(obj);
});
Source: Stackoverflow.com