1

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?

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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}
});

Comments

0

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.