Reputation: 1159
I have an object array:
source_array = [
{
"prop1":"one",
"prop2":"two",
"prop3":"three"
},
{
"prop1":"four",
"prop2":"five",
"prop3":"six"
}
]
and declaration of destination_array
:
destination_array: { prop1: any, prop2: any }[];
Now I want to populate destination_array
so that I only take prop1
and prop2
from source_array
like this:
[
{
"prop1":"one",
"prop2":"two"
},
{
"prop1":"four",
"prop2":"five"
}
]
I tried this:
this.objectArray = new Array(this.performances.length)
this.performances.filter(item => {
this.objectArray.fill({'prop1':item.prop1, 'prop2':item.prop2})
});
but this will only return:
[
{
"prop1":"four",
"prop2":"five"
},
{
"prop1":"four",
"prop2":"five"
}
]
what am I doing wrong?
Upvotes: 1
Views: 98
Reputation: 717
I think what you're looking for is Array.map function
const destination_array = source_array.map(item => {
return {prop1: item.prop1, prop2: item.prop2}
});
Upvotes: 1
Reputation: 222582
Use Array.Map()
var source_array = [
{
"prop1":"one",
"prop2":"two",
"prop3":"three"
},
{
"prop1":"four",
"prop2":"five",
"prop3":"six"
}
];
var results = source_array.map(function(item){
return {prop1 : item["prop1"], prop2 : item["prop2"]}
});
console.log(results);
Upvotes: 0
Reputation: 1569
Try with this:
this.destinations = this.performances.map(performance => ({
prop1: performance.prop1,
prop2: performance.prop2
}));
console.log(this.destinations);
A working example: https://stackblitz.com/edit/angular-filter-array-properties?file=src/app/app.component.ts
Upvotes: 2