简体   繁体   中英

Return filtered array js

I have an Array of Objects, each containing Array and Objects, like so:

data = [{
    "id": 10022,
    "date": "2017-12-31T03:44:19.963808Z",
    "bought_beats": [{
        "id": 10034,
        "beat": {
            "id": 6334,
            "name": "Glass",
            "producer": {
                "id": 23,
                "display_name": "MadReal",
            }
        },
        "license": {
            "id": 10034,
            "name": "Premium",
        },
    }, {
        "id": 894,
        "beat": {
            "id": 6334,
            "name": "Other Name",
            "producer": {
                "id": 25,
                "display_name": "Other Name",
            }
        },
        "license": {
            "id": 10034,
            "name": "Premium",
        },
    }]
}, {
    "moredata": "stuff"
}]

And I need to filter the bought_beats property, and only return beat , if beat.producer.id === 23

This is what I have but it's clearly not working

data.forEach(order => {
    return order.bought_beats.filter(item => item.beat.id === producerId)
})    

===========

Edit1:

Trying this. It "works", but it also removed some properties (id & date) from each order object (which is each index of data ), so I have objects that only contain the array of "bought_beats"

var res = data.map(item => item.bought_beats.filter(item => item.beat.producer.id === 23))

========

Edit2

This seems to be 1 solution, it maintains the array and object structure the same, while it removes those unwanted elements from the bought_beats array.

data.forEach(order => {
    let elementToRemoveIndex = order.bought_beats.findIndex(item => item.beat.producer.id !== 23)
    order.bought_beats.splice(elementToRemoveIndex, 1)
})

Thanks @Pac0 for the continuous help

use .find over data.bought_beats since its an array,

DEMO

 var data = [{ "id": 10022, "date": "2017-12-31T03:44:19.963808Z", "bought_beats": [{ "id": 10034, "beat": { "id": 6334, "name": "Glass", "producer": { "id": 23, "display_name": "MadReal", } }, "license": { "id": 10034, "name": "Premium", }, }, { "id": 894, "beat": { "id": 6334, "name": "Other Name", "producer": { "id": 25, "display_name": "Other Name", } }, "license": { "id": 10034, "name": "Premium", }, }] }, { "moredata": "stuff" }]; var result = data.find(dat => dat.bought_beats.some(item => item.beat.producer.id === 23)); console.log(result); 

If I understood correctly, this should be what you want :

// project each object to its bought_beats / beats part
var beatsArrays = data.filter(x => x.bought_beats).map(x => x.bought_beats); 

// flatten the array of arrays of beats into a simple array of beats
var beats = [].concat.apply([],beatsArrays).map(x => x.beat); 

// filter
var relevantBeats = beats.filter(item => item.producer.id === 23); 

// serve with a cherry in a sugar-frost cocktail glass (happy new year ! )
console.log(relevantBeats);

Snippet :

 data = [{ "id": 10022, "date": "2017-12-31T03:44:19.963808Z", "bought_beats": [{ "id": 10034, "beat": { "id": 6334, "name": "Glass", "producer": { "id": 23, "display_name": "MadReal", } }, "license": { "id": 10034, "name": "Premium", }, }, { "id": 894, "beat": { "id": 6334, "name": "Other Name", "producer": { "id": 25, "display_name": "Other Name", } }, "license": { "id": 10034, "name": "Premium", }, }] }, { "moredata": "stuff" }]; // project each object to its bought_beats / beats part var beatsArrays = data.filter(x => x.bought_beats).map(x => x.bought_beats); // flatten the array of arrays of beats into a simple array of beats var beats = [].concat.apply([],beatsArrays).map(x => x.beat); // filter var relevantBeats = beats.filter(item => item.producer.id === 23); // serve with a cherry in a sugar-frost cocktail glass (happy new year ! ) console.log(relevantBeats); 

// for each order
data.forEach(order => {
    // we loop thorugh the bought beats array
    order.bought_beats.forEach((item, index) => {
        // and if there's a beat from another producer, we remove it
        if (item.beat.producer.id !== producerId) order.bought_beats.splice(index, 1)
    })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM