Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组数组转换为javascript中的单个对象数组_Javascript_Arrays_Node.js_Sorting_Lodash - Fatal编程技术网

数组数组转换为javascript中的单个对象数组

数组数组转换为javascript中的单个对象数组,javascript,arrays,node.js,sorting,lodash,Javascript,Arrays,Node.js,Sorting,Lodash,我有一个数组,如下所示:- [ { subsets: [ { start: '0020', end: '007F', }, { start: '0020', end: '007G',

我有一个数组,如下所示:-

[
        {
            subsets:
                [
                    {
                        start: '0020', end: '007F',
                    },
                    {
                        start: '0020', end: '007G',
                    },
                    {
                        start: '0020', end: '007G',
                    },
                ],
            fontFormat: 'woff2',
            src: 'estPath1/',
        },
        {
            subsets:
                [
                    {
                        start: '0020', end: '007F',
                    },
                    {
                        start: '0020', end: '007G',
                    },
                    {
                        start: '0020', end: '007G',
                    },
                ],
            fontFormat: 'woff',
            src: 'estPath2/',
        },

    ],
如果在数组的两个对象中都看到子集数组键相同。我需要得到的输出应该如下所示:-

[
    {
        subset: [
            {
                start: '0020', end: '007F',
            },
            {
                start: '0020', end: '007G',
            },
            {
                start: '0020', end: '007G',
            },
        ],
        webFontList: [
            {
                fontFormat: 'woff2',
                src: 'estPath1/',
            },
            {
                fontFormat: 'woff',
                src: 'estPath2/',
            },
        ],

    },
];
我在javascript中使用lodash,有谁能告诉我高效的解决方案吗

我的方法是使用lodash uniqueby函数从上面的数组中获取唯一的子集数组,然后从具有子集键的主数组中获取所有对象,并生成自定义对象

这不是一个有效的解决方案,因此需要寻找更多的想法。

您可以使用它来构建新数组,使用lodash来检查是否已经找到相同的子集

const数据=[{
子集:[{
开始:“0020”,结束:“007G”,
}, {
开始:“0020”,结束:“007F”,
},{
开始:“0020”,结束:“007G”,
}],
fontFormat:'woff2',
src:'estPath1/',
},{
子集:[{
开始:“0020”,结束:“007F”,
},{
开始:“0020”,结束:“007G”,
},{
开始:“0020”,结束:“007G”,
}],
fontFormat:“woff”,
src:'estPath2/',
}];
常量合并=(数据)=>
data.reduce((acc,{subset,fontFormat,src})=>{
const found=acc.find({subset})=>
_isEqual先生(
_.sortedIndexBy(子集,({start,end})=>start-end),
_.sortedIndexBy(子集,({start,end})=>start-end))
);
if(!found)acc.push({subset:subsets,webFontList:[{fontFormat,src}]});
else found.webFontList.push({fontFormat,src});
返回acc;
},
[]);
console.log(合并(数据))

此代码有助于您合并阵列中的所有数据:

let test = [
        {
            subsets:
                [
                    {
                        start: '0020', end: '007F',
                    },
                    {
                        start: '0020', end: '007G',
                    },
                    {
                        start: '0020', end: '007G',
                    },
                ],
            fontFormat: 'woff2',
            src: 'estPath1/',
        },
        {
            subsets:
                [
                    {
                        start: '0020', end: '007F',
                    },
                    {
                        start: '0020', end: '007G',
                    },
                    {
                        start: '0020', end: '007G',
                    },
                ],
            fontFormat: 'woff',
            src: 'estPath2/',
        },
        {
            subsets:
                [
                    {
                        start: '0020', end: '007F',
                    },
                    {
                        start: '0021', end: '007G',
                    },
                    {
                        start: '0020', end: '007G',
                    },
                ],
            fontFormat: 'woff',
            src: 'estPath3/',
        }
    ];
    var final = [];
    for(var i = 0; i < test.length; i++) {
        var object = {subsets : [] , webFontList: []};
        object.subsets = test[i].subsets;
        object.webFontList.push({fontFormat: test[i].fontFormat, src: test[i].src});
        for(var j = i +1; j < test.length; j++) {
            if(_.isEqual(test[i].subsets, test[j].subsets)) {
                object.webFontList.push({fontFormat: test[j].fontFormat, src: test[j].src});
                test.splice(j, 1);
            }
        }
        final.push(object);
    }
    console.log("final object is : ", final);
let测试=[
{
子集:
[
{
开始:“0020”,结束:“007F”,
},
{
开始:“0020”,结束:“007G”,
},
{
开始:“0020”,结束:“007G”,
},
],
fontFormat:'woff2',
src:'estPath1/',
},
{
子集:
[
{
开始:“0020”,结束:“007F”,
},
{
开始:“0020”,结束:“007G”,
},
{
开始:“0020”,结束:“007G”,
},
],
fontFormat:“woff”,
src:'estPath2/',
},
{
子集:
[
{
开始:“0020”,结束:“007F”,
},
{
开始:“0021”,结束:“007G”,
},
{
开始:“0020”,结束:“007G”,
},
],
fontFormat:“woff”,
src:'estPath3/',
}
];
var final=[];
对于(变量i=0;i
以下是解决方案:

 const fontList = params.fontList.map((font) => {
        const uniquesubset = _.compact(_.map(_.uniqWith(font.webFontList, (item, itemTwo) => _.isEqual(item.subset, itemTwo.subset)).map(_.property('subset'))));
        if (uniquesubset.length > 0) {
            const subsets = uniquesubset.map((subset) => {
                const webFontList = _.compact(_.map(font.webFontList, webFont => (_.isEqual(webFont.subset, subset) ? _.pick(webFont, ['fontFormat', 'src', ]) : undefined)));
                const updatedWebFontList = _.compact(webFontList);
                return {
                    subset,
                    webFontList: updatedWebFontList,
                };
            });
            return {
                subsets,
                sourceFont: font.sourceFont,
            };
        }
        return {
            sourceFont: font.sourceFont,
            subsets: {
                webFontList: font.webFontList,
            },
        };
    });


我会进一步优化它。但是它确实提供了我所需要的。

有人能优化这个解决方案吗?从某种意义上说,我需要使用lodash编写更少的代码,或者如果性能可以优化,那就太好了!!