-1
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 always adds in the last element of each object
Example here: https://jsfiddle.net/a6dx81fy/
I wanted the object to be added to 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" }];
carlos, do not abandon your questions. Mark an answer as the right one, or if none solved, question. Abandoning questions is not part of the community’s purpose. Abs!
– Sam