How to push an array of object as key value pair into an array
You are using the key from the JSON as the index in your array. Try this.
Object.keys(this.lineItem.rows).forEach(key => {
var obj = {};
obj[key] = this.lineItem.rows[key];
this.resultArray.push(obj);
});
how to push value pair into object using method?
You need to check if the key exist, and then either push or assign the value, like this, where you use the bracket notation, this.rosterList[grade]
, to set the key.
Note, when assign with Arry.push(value)
you will get the length of the array, not the array itself.
Stack snippet
class School{ constructor(){ this.rosterList = {}; } roster(){ return this.rosterList; } add(name,grade){ this.rosterList[grade] ? this.rosterList[grade].push(name) : this.rosterList[grade] = [name]; }}
let a = new School;a.add('tom', 2)a.add('amy', 2)console.log(a.roster());
Adding elements to object
Your element is not an array, however your cart needs to be an array in order to support many element objects. Code example:
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push(element);
If you want cart to be an array of objects in the form { element: { id: 10, quantity: 1} }
then perform:
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push({element: element});
JSON.stringify()
was mentioned as a concern in the comment:
>> JSON.stringify([{a: 1}, {a: 2}])
"[{"a":1},{"a":2}]"
How to push object literals as key-value pairs to array?
You can achieve the required result using plain JS:
Object.keys(myObject).map(key => ({[key]: myObject[key]}));
This approach uses computed property names of ES2015.
As suggested in comments, you can use Object.entries
as well:
Object.entries(myObject).map(([p, v]) => ({[p]: v}))
If you want to use lodash anyway, you should surround key
with square brackets:
const newArray = _.map(myObject, (value, key) => ({[key], value}))
Javascript Object push() function
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;
Related Topics
Create a Newline for Every X Number of Characters
Can You Combine Multiple Images into a Single One Using JavaScript
Starting the Week on Monday With Isoweekday()
Postman Test - Validating String Values in an Object in an Array
Can We Insert JavaScript into Any Webpage Loaded in the Browser
How to Count Duplicate Value in an Array in JavaScript
Update Url With Value from Input on Click With React
Prevent Bootstrap Modal Window from Closing on Form Submission
How to Add Space Between Every 4 Characters in JavaScript
Javascript Generate Unique Number Based on String
Set Focus to the Next Input Element
Blob Createobjecturl Download Not Working in Firefox (But Works When Debugging)
Hosting Your Own JavaScript Scripts Files (Other Than Jquery) on Fast Free Cdns Like Google
Image Upload With Preview and Delete Option - Javascript/Jquery
How to Set the Multi Sliders in One Page