Javascript 如何在已经声明的 JSON 对象中添加键值对
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28527712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add key value pair in the JSON object already declared
提问by MaxSteel
I have declared a JSON Object and added some key value pair in that like:
我已经声明了一个 JSON 对象并在其中添加了一些键值对,例如:
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.
但是我想在同一个对象中添加更多的键值对,如果我添加上面提到的键值对,那么它会替换旧的。那么任何人都可以告诉我如何在同一个 JSON 对象中附加数据,即 obj。
回答by scgough
Could you do the following:
你能不能做到以下几点:
obj = {
"1":"aa",
"2":"bb"
};
var newNum = "3";
var newVal = "cc";
obj[newNum] = newVal;
alert(obj["3"]); // this would alert 'cc'
回答by rfornal
You can use dot notation or bracket notation ...
您可以使用点符号或括号符号...
var obj = {};
obj = {
"1": "aa",
"2": "bb"
};
obj.another = "valuehere";
obj["3"] = "cc";
回答by Manish
Hi I add key and value to each object
嗨,我为每个对象添加键和值
let persons = [
{
name : "John Doe Sr",
age: 30
},{
name: "John Doe Jr",
age : 5
}
]
function addKeyValue(obj, key, data){
obj[key] = data;
}
let newinfo = persons.map(function(person) {
return addKeyValue(person, 'newKey', 'newValue');
});
console.log(persons);
回答by sagar buddhi
Object assign copies one or more source objects to the target object. So we could use Object.assignhere.
对象分配将一个或多个源对象复制到目标对象。所以我们可以Object.assign在这里使用。
Syntax: Object.assign(target, ...sources)
句法: Object.assign(target, ...sources)
var obj = {};
Object.assign(obj, {"1":"aa", "2":"bb"})
console.log(obj)
回答by shrawan_lakhe
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:
下面是示例演示的代码和 jsfiddle 链接,单击按钮时,它将向已存在的 obj 添加更多键值对:
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);
});
回答by Harendra Kr. Jadon
Please try following simple operations on a json, insert/update/push:
请尝试对 json 执行以下简单操作,插入/更新/推送:
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);
回答by sgajera
possible duplicate , best way to achieve same as stated below:
可能重复,实现相同的最佳方法如下所述:
function getKey(key) {
return `${key}`;
}
var obj = {key1: "value1", key2: "value2", [getKey('key3')]: "value3"};

