返回已过滤的数组js

时间:2017-12-31 17:04:29

标签: javascript arrays

我有一个对象数组,每个对象包含数组和对象,如下所示:

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"
}]

我需要过滤bought_beats属性,只返回beat,如果beat.producer.id === 23

这就是我所拥有的,但它显然不起作用

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

===========

EDIT1:

试试这个。它"工作",但它也从每个订单对象(每个data的索引)中删除了一些属性(id& date),所以我有只包含&的数组的对象#34; bought_beats"

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

======

EDIT2

这似乎是一个解决方案,它保持数组和对象结构相同,同时从bought_beats数组中删除那些不需要的元素。

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

感谢@ Pac0的持续帮助

3 个答案:

答案 0 :(得分:0)

对data.bought_beats使用 .find ,因为它是一个数组,

<强>样本

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

答案 1 :(得分:0)

如果我理解正确,这应该是你想要的:

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

摘录:

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

答案 2 :(得分:0)

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