Javascript Object push() function

JavascriptJson

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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

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;

Solution 2 - Javascript

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 –

Solution 3 - Javascript

You must make var tempData = new Array();

Push is an Array function.

Solution 4 - Javascript

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

Solution 5 - Javascript

I hope this one might help you.

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)

Solution 6 - Javascript

    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.

Solution 7 - Javascript

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

Solution 8 - Javascript

Do :


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


Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew JackmanView Question on Stackoverflow
Solution 1 - JavascriptMatt BallView Answer on Stackoverflow
Solution 2 - JavascriptcsantanaView Answer on Stackoverflow
Solution 3 - JavascriptAlex DnView Answer on Stackoverflow
Solution 4 - JavascriptregexView Answer on Stackoverflow
Solution 5 - JavascriptProsanta ChakiView Answer on Stackoverflow
Solution 6 - JavascriptRollingInTheDeepView Answer on Stackoverflow
Solution 7 - JavascriptKamil KiełczewskiView Answer on Stackoverflow
Solution 8 - JavascriptSudhir BastakotiView Answer on Stackoverflow