比较一个数组和一个对象数组,并根据指定的值将内容推送到新数组中 [英] Compare a array and an object array and push the contents into the new array according to the value specified

查看:37
本文介绍了比较一个数组和一个对象数组,并根据指定的值将内容推送到新数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

data = [ '1', '2','2] objlist = [ { name : 'dummy' } , {name: 'new' }, { name : 'news'},{name :'地方'}....] - 5 个对象

result = [ [{name:'dummy'}], [{name:'new'},{name:'news'}],[{name:'place'},...]]

所需的结果是所需的结果数组.由于我是新手,我无法继续进行下去,而且我也怀疑这是否可能,如果是建议或 JavaScript 中的工作代码会有所帮助,谢谢

the desired result is the result array required. I am not able to proceed further as I am new and also have a doubt like is this possible or not, if it is suggestions or working code in javascript would be helpful, thank you

例如,如果数组数据的值为 ['1','2','2'],则我想要的结果对象数组应将所有元素放在一个位置,然后将其切片并制作成这个 - [[{obj1}],[{obj2},{obj3}],[{obj4},{obj5}]]

what i wanted for example if array data has values as ['1','2','2'] the result object array which has all the elements in one place should be sliced and made into this - [[{obj1}],[{obj2},{obj3}],[{obj4},{obj5}]]

推荐答案

您可以将切片部分推送到结果中.

You could push sliced parts to the result.

let array = [1, 2, 2],
    objlist = [{ name: 'dummy' }, { name: 'new' }, { name: 'news' }, { name: 'place' }, { name: 'place' }],
    result = [],
    i = 0,
    j = 0;
    
while (j < array.length) {
    result.push(objlist.slice(i, i += array[j++]));
}

console.log(result);

这篇关于比较一个数组和一个对象数组,并根据指定的值将内容推送到新数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆