简体   繁体   English

过滤和连接对象 javascript 的 arrays

[英]filter and concat arrays of objects javascript

i am trying to filter some objects that i have in an array, but inside this array i have some objects that i would like to collect and concat as array, i am working arround but i couldn't reach the expected output我正在尝试过滤我在数组中拥有的一些对象,但在这个数组中我有一些我想收集并连接为数组的对象,我正在工作,但我无法达到预期的 output

MY ARRAY我的阵列

[
    {
        _id: '60c0c5d7f5b87a1604d3d555',
        name: 'Colors'
        attribute: {
            value: 'red'
            _id: '60c0c5d7f5b87a1604d3d544',
        }
    },
    {
        _id: '60c0c5d7f5b87a1604d3d555',
        name: 'Colors'
        attribute: {
            value: 'blue'
            _id: '60c0c5d7f5b87a1604fsd33',
        }
    },
    {
        _id: '60c0c5d7f5b87a1604d3f566',
        name: 'Sizes'
        attribute: {
            value: 'M'
            _id: '60c0c5d7f5b87a1604d3d522',
        }
    },
    {
        _id: '60c0c5d7f5b87a1604d3f566',
        name: 'Sizes'
        attribute: {
            value: 'L'
            _id: '60c0c5d7f5b87a1604d3d512',
        }
    }
]

EXPECTED OUTPUT预期 OUTPUT

[
    {
        _id: '60c0c5d7f5b87a1604d3d555',
        name: 'Colors'
        attributes:[
            {
                value: 'red'
                _id: '60c0c5d7f5b87a1604d3d544',
            },
             {
                value: 'blue'
                _id: '60c0c5d7f5b87a1604fsd33',
            }
        ]
    },
    {
        _id: '60c0c5d7f5b87a1604d3f566',
        name: 'Sizes'
        attributes: [
            {
                value: 'M'
                _id: '60c0c5d7f5b87a1604d3d522',
            },
            {
                value: 'L'
                _id: '60c0c5d7f5b87a1604d3d512',
             }
        ]
    }
]

Any help is appreciated, thanks in advance!任何帮助表示赞赏,在此先感谢!

You can use array#reduce and object.values() to group your array based on _id .您可以使用array#reduceobject.values()根据_id对数组进行分组。

 const data = [ { _id: '60c0c5d7f5b87a1604d3d555', name: 'Colors', attribute: { value: 'red', _id: '60c0c5d7f5b87a1604d3d544', } }, { _id: '60c0c5d7f5b87a1604d3d555', name: 'Colors', attribute: { value: 'blue', _id: '60c0c5d7f5b87a1604fsd33', } }, { _id: '60c0c5d7f5b87a1604d3f566', name: 'Sizes', attribute: { value: 'M', _id: '60c0c5d7f5b87a1604d3d522', } }, { _id: '60c0c5d7f5b87a1604d3f566', name: 'Sizes', attribute: { value: 'L', _id: '60c0c5d7f5b87a1604d3d512', } } ], result = Object.values(data.reduce((r, {_id, name, attribute}) => { r[_id] = r[_id] || { _id, name, attributes: []}; r[_id].attributes.push(attribute); return r; },{})); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

Can try to create hashmap list, with id+name identifier and value attribute list.可以尝试创建 hashmap 列表,带有 id+name 标识符和 value 属性列表。 I think that this way you will be able to concatenate and have the expected output.我认为这样你将能够连接并拥有预期的 output。

function concatArray(idTextAreaOut){
    var i =0,j = 0;
    var array = [ { _id: '60c0c5d7f5b87a1604d3d555', name: 'Colors', attribute: { value: 'red', _id: '60c0c5d7f5b87a1604d3d544', } }, { _id: '60c0c5d7f5b87a1604d3d555', name: 'Colors', attribute: { value: 'blue', _id: '60c0c5d7f5b87a1604fsd33', } }, { _id: '60c0c5d7f5b87a1604d3f566', name: 'Sizes', attribute: { value: 'M', _id: '60c0c5d7f5b87a1604d3d522', } }, { _id: '60c0c5d7f5b87a1604d3f566', name: 'Sizes', attribute: { value: 'L', _id: '60c0c5d7f5b87a1604d3d512', } } ];
    var arrayConcat = [];
    for(i = 0; i < array.length;i++){
        var element = array[i];
        var key = element._id+element.name;
        var finded = false;
        for(j = 0; j < arrayConcat.length;j++){
            var otherElement = arrayConcat[j];
            var keyOther = otherElement._id+otherElement.name;
            if(key == keyOther){
                if(!Array.isArray(otherElement.attribute)){
                    var temp = otherElement.attribute;
                    otherElement.attribute = [];
                    otherElement.attribute.push(temp);
                }
                otherElement.attribute.push(element.attribute);
                finded = true;
            }
        }

        if(!finded){
            arrayConcat.push(element);
        }
        
    }
    $('#'+idTextAreaOut).val(JSON.stringify(arrayConcat));
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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