Javascript Object push() function

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

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.push is not a function. I understand why it isn't the same as an array, but what could I do otherwise?

853767 次浏览

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;

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

Push 是一个 Array 函数。

做:


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

    tempData.push( data[index] );

我同意上面的正确答案,但是... ..。仍然没有给出要添加到 temData 的数据的索引值。如果没有[ index ]值,则会添加整个数组。

对象不支持 push 属性,但也可以使用索引作为键保存该属性,

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

我认为这是比较容易的,如果删除对象的状态是无效的,通过做。

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

最后,您不需要创建 var temp-

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

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

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

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);

我希望这个能帮到你。

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


let tempData = [];


tempData= data.filter((item)=>item.Status!='Invalid')


console.log(tempData)

Mozilla actually shows you how to handle objects with push by chaining push to the call method:

"push is intentionally generic, and we can use that to our advantage. Array.prototype.push can work on an object just fine, as this example shows.

注意,我们不创建数组来存储对象集合。相反,我们将集合存储在对象本身上,并使用 Array.Prototype.push 调用来欺骗该方法,让它认为我们正在处理的是一个数组ーー而且它确实有效,这要感谢 JavaScript 允许我们以任何我们想要的方式建立执行上下文的方式。

const obj = {
length: 0,


addElem(elem) {
// obj.length is automatically incremented
// every time an element is added.
[].push.call(this, elem);
},
};


// Let's add some empty objects just to illustrate.
obj.addElem({});
obj.addElem({});
console.log(obj.length);
// → 2

注意,虽然 obj 不是一个数组,但是这个方法成功地增加了 obj 的长度属性,就像我们处理一个实际的数组一样。”

Https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/push