var fields = [
{
"Category": "Computers",
"Price": "125.60",
"Book ID": "1",
"Book Name": "Computer Architecture"
},
{
"Book ID": "2",
"Category": "Programming",
"Price": "56.00",
"Book Name": "Asp.Net 4 Blue Book"
},
{
"Price": "210.40",
"Book ID": "3",
"Category": "Science",
"Book Name": "Popular Science"
}
];
if(fields.length > 0){
var newField = [];
for(var i = 0; i < fields.length; i++){
var field = fields[i];
fields[i]["abobora"] = 'abobora teste';
}
var newField = fields;
console.log(newField);
}
- How do I add in the second position of each element? The above example I've always added adds in the last element of each object
Example here: link
-
I wanted the object to be added in a given index, as an example below:
var fields = [ { "Category": "Computers", "Price": "125.60", "abobora": 'abobora teste', <- gerar nesse index "Book ID": "1", "Book Name": "Computer Architecture" }, { "Book ID": "2", "Category": "Programming", "abobora": 'abobora teste', <- gerar nesse index "Price": "56.00", "Book Name": "Asp.Net 4 Blue Book" }, { "Price": "210.40", "Book ID": "3", "abobora": 'abobora teste', <- gerar nesse index "Category": "Science", "Book Name": "Popular Science" }];