javascript 如何在javascript中更新对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17740904/
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 update an array of object in javascript
提问by paynestrike
say I have a variable
说我有一个变量
data=[]
//this variable is an array of object like
data = [
{name:'a',value:'aa'},
{name:'b',value:'bb'}
]
// the data structrue can not be changed and the initial value is empty
now I want to update the data
现在我想更新数据
function updateData(){
if(!data.length){
data.push(arguments)
}
else{
//this parts really confuse me
}
}
this function must accept any number of arguments,and the order of the objects in data dose not matters update rule:
此函数必须接受任意数量的参数,并且数据中对象的顺序无关紧要 更新规则:
- update the objects value to the arguments value if they have the same name.
- add the arguments to the data if none of the object in data have the same name.
- 如果对象具有相同的名称,则将对象值更新为参数值。
- 如果 data 中的任何对象都没有同名,则将参数添加到 data 中。
how to write this function?
这个函数怎么写?
updateData([
{name:'a',value:'aa'},
{name:'b',value:'bb'}
])
// expect data = [
{name:'a',value:'aa'},
{name:'b',value:'bb'}
]
updateData([
{name:'a',value:'aa'},
{name:'b',value:'DD'},
{name:'c',value:'cc'}
] )
// expect data = [
{name:'a',value:'aa'},
{name:'b',value:'DD'},
{name:'c',value:'cc'}
]
采纳答案by HMR
As Ashutosh Upadhyay suggested use a name value pair instead of an array:
正如 Ashutosh Upadhyay 建议的那样,使用名称值对而不是数组:
var data ={};
var updateData=function(){
var len = arguments.length,
i=0;
for(i=0;i<len;i++){
data[arguments[i].name]=arguments[i].value;
}
};
updateData(
{name:"a",value:"22"},
{name:"b",value:"2"},
{name:"c",value:"3"},
{name:"a",value:"1"} // the new value for a
);
for(key in data){
if(data.hasOwnProperty(key)){
console.log(key + "=" + data[key]);
}
}
Just read your comment about the array, so if you have to have an array you can:
只需阅读您对数组的评论,因此如果您必须拥有一个数组,您可以:
var data = [];
function findIndex(name){
var i = 0;
for(i=0;i<data.length;i++){
if(data[i].name===name){
return i;
}
}
return i;
}
function updateData(){
var i = 0;
for(i=0;i<arguments.length;i++){
data[findIndex(arguments[i].name)]=arguments[i];
}
}
updateData(
{name:"a",value:"22"},
{name:"b",value:"2"},
{name:"c",value:"3"},
{name:"a",value:"1"} // the new value for a
);
console.log(data);
回答by Romaindr
Ok you might want to do something like that?
好吧,你可能想做这样的事情?
var data = [
{name:'a',value:'aa'},
{name:'b',value:'bb'}
];
function updateData(obj){
var objFound_bool = false;
for (var i = 0; i < data.length; i++) {
if(obj.name === data[i].name){
objFound_bool = true;
data[i].value =obj.value ;
}
}
if(!objFound_bool){
data.push(obj)
}
}
回答by Ashutosh Upadhyay
Since order doesn't really matter to you, better choice would be an object hash instead of an array. Something like
由于顺序对您来说并不重要,因此更好的选择是对象哈希而不是数组。就像是
{
'a':{name:'a', value: 'aa'}
}
This will let you search easily based on keys and you can update the hash.
这将使您可以轻松地根据键进行搜索,并且可以更新哈希。
You can even employ above to your situation. Convert the array into a temporary object hash as above, do necessary changes, and convert back to an array. This will be more efficient as it will save you searches through the array.
您甚至可以根据自己的情况使用上述内容。如上所述将数组转换为临时对象哈希,进行必要的更改,然后转换回数组。这将更有效,因为它可以节省您在数组中的搜索。
var output_array = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
output_array.push(obj[key]);
}
}
回答by Pablo Lozano
if names are unique you could define data
as a map and just add the values using the name as key:
如果名称是唯一的,您可以将其定义data
为映射,然后使用名称作为键添加值:
var data={
'a':'aa',
'b':'bb'
}
function updateData() {
for (var i=0;i<arguments.length;i++) { //for each argument...
for (key in arguments[i]) { //we'll put each pair (key,value) in the data
data[key]=arguments[i][key];
}
}
}
EDIT: function to transform to an array
编辑:转换为数组的函数
function toArray() {
var array=[];
for (key in data) {
array.push({name:key,value:data[key]});
}
return array;
}