Javascript 对象 push() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8925820/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:06:28  来源:igfitidea点击:

Javascript Object push() function

javascriptjson

提问by Andrew Hymanman

I have a javascript object (I actually get the data through an ajax request):

我有一个 javascript 对象(我实际上是通过 ajax 请求获取数据的):

var data = {};

I have added some stuff into it:

我在里面添加了一些东西:

data[0] = { "ID": "1"; "Status": "Valid" }
data[1] = { "ID": "2"; "Status": "Invalid" }

Now I want to remove all objects with an invalid status (but keep everything the ordering same):

现在我想删除所有状态无效的对象(但保持所有内容的顺序相同):

var tempData = {};
for ( var index in data ) {
    if ( data[index].Status == "Valid" ) {
        tempData.push( data );
    }
}
data = tempData;

In my mind, all of this should work, but I am getting an error that tempData.pushis not a function. I understand why it isn't the same as an array, but what could I do otherwise?

在我看来,所有这些都应该有效,但我收到了一个tempData.push不是函数的错误。我明白为什么它与数组不同,但我还能做什么?

回答by Matt Ball

push()is for arrays, not objects, so use the right data structure.

push()用于数组,而不是对象,因此请使用正确的数据结构。

var data = [];
// ...
data[0] = { "ID": "1", "Status": "Valid" };
data[1] = { "ID": "2", "Status": "Invalid" };
// ...
var tempData = [];
for ( var index=0; index<data.length; index++ ) {
    if ( data[index].Status == "Valid" ) {
        tempData.push( data );
    }
}
data = tempData;

回答by csantana

Objects does not support push property, but you can save it as well using the index as key,

对象不支持推送属性,但您也可以使用索引作为键来保存它,

var tempData = {};
for ( var index in data ) {
  if ( data[index].Status == "Valid" ) { 
    tempData[index] = data; 
  } 
 }
data = tempData;

I think this is easier if remove the object if its status is invalid, by doing.

我认为如果删除对象,如果其状态无效,这样做会更容易。

for(var index in data){
  if(data[index].Status == "Invalid"){ 
    delete data[index]; 
  } 
}

And finally you don't need to create a var temp –

最后你不需要创建一个 var temp –

回答by Alex Dn

You must make var tempData = new Array();

你必须使 var tempData = new Array();

Push is an Array function.

Push 是一个数组函数。

回答by regex

Javascript programming language supports functional programming paradigm so you can do easily with these codes.

Javascript 编程语言支持函数式编程范式,因此您可以轻松地使用这些代码。

var data = [
    {"Id": "1", "Status": "Valid"},
    {"Id": "2", "Status": "Invalid"}
];
var isValid = function(data){
    return data.Status === "Valid";
};
var valids = data.filter(isValid);

回答by Kamil Kie?czewski

I assume that REALLY you get object from server and want to get object on output

我假设您真的从服务器获取对象并希望在输出中获取对象

Object.keys(data).map(k=> data[k].Status=='Invalid' && delete data[k])

var data = { 5: { "ID": "0", "Status": "Valid" } }; // some OBJECT from server response

data = { ...data,
  0: { "ID": "1", "Status": "Valid" },
  1: { "ID": "2", "Status": "Invalid" },
  2: { "ID": "3", "Status": "Valid" }
}

// solution 1: where output is sorted filtred array
let arr=Object.keys(data).filter(k=> data[k].Status!='Invalid').map(k=>data[k]).sort((a,b)=>+a.ID-b.ID);
  
// solution2: where output is filtered object
Object.keys(data).map(k=> data[k].Status=='Invalid' && delete data[k])
  
// show
console.log('Object',data);
console.log('Array ',arr);

回答by user3094826

    tempData.push( data[index] );

I agree with the correct answer above, but.... your still not giving the index value for the data that you want to add to tempData. Without the [index] value the whole array will be added.

我同意上面的正确答案,但是.... 您仍然没有为要添加到 tempData 的数据提供索引值。如果没有 [index] 值,整个数组将被添加。

回答by Sudhir Bastakoti

Do :

做 :


var data = new Array();
var tempData = new Array();