I have an array like this:
var records = [{
"field1": "dogs",
"field2": "poodle"
}, {
"field1": "dogs",
"field2": "alsatian"
}, {
"field1": "dogs",
"field2": "chowchow"
}, {
"field1": "dogs",
"field2": "schnauzer"
}, {
"field1": "cats",
"field2": "siamese"
}, {
"field1": "cats",
"field2": "persian"
}, {
"field1": "fish",
"field2": "guppy"
}, {
"field1": "fish",
"field2": "tiger barb"
}]
I want to iterate over the array and create a new array Pets, so that I can access it like
var Pets = [{
"type": "dogs",
"breeds": ["poodle", "alsatian", "chowchow", "schnauzer"]
}, {
"type": "cats",
"breeds": ["siamese", "persian"]
}, {
"type": "fish",
"breeds": ["guppy", "tiger barb"]
}]
I tried to do a for loop but it doesn't work as I use i+1 in an array like so
var thispet = {};
var Pets = [];
thispet.type = records[0].field1;
Pets.push(thispet);
for (i = 1; i < records.length; i++) {
if (Pets[i - 1].type != records[i].field1) {
thispet.type = records[i] field1;
Pets.push(thispet);
}
}
But somehow the Pets[i-1].type is not recognised as object.
I wanted to first create 3 objects in array Pets based on the 3 types of pets in the records, then sort out the second order of breeds(should be easier with push into array. I have a large record, so a loop will help greatly.