简体   繁体   English

使用数组创建自定义的二级对象-Javascript

[英]create custom two level object using arrays - Javascript

I have to create a custom object using two arrays which contains objects with two to three properties. 我必须使用两个包含两个到三个属性的对象的数组来创建一个自定义对象。 Now based on number of matched items I have create the object. 现在,基于匹配项的数量,我创建了对象。 Here I don't want to use too many variables. 在这里,我不想使用太多变量。 Without using intermediate variables I want to write the code 不使用中间变量,我想编写代码

code

var fn = function() {
  var list1 = [{aId: 0, name: 'item1'},
             {aId: 1, name: 'item2'},
             {aId: 2, name: 'item3'}],
    list2 = [ { id: 0, label: 'one', actions: [ 0, 1, 2 ] },
              { id: 1, label: 'two', actions: [ 0, 2 ] } ],
    customObj ={};
    for(var i=0; i<list1.length; i++) {
        for(var j=0; j<list2.length; j++){
             customObj[list2[j].id] = {};
             for(var k=0; k<list2[j].actions.length; k++){
                 if(list1[i].aId == list2[j].actions[k]){
                     customObj[list2[j].id][list1[i].name] = true

                 }
             }
        }
    }
   return customObj;
}

Required Output: 要求的输出:

customObj = {
   0: {
      item1: true,
      item2: true,
      item3: true
   },
   1: {
     item1:true,
     item3:true
   }
}

Can anyone suggest me where I am doing mistake? 谁能建议我在哪里做错了?

You're assigning a value to the entire object instead of just the one property. 您正在为整个对象分配一个值,而不只是一个属性。 Inside your condition, you just need customObj[list2[j].id][list1[i].name] = true . 在您的条件内,您只需要customObj[list2[j].id][list1[i].name] = true Using brackets to access the properties (as opposed to dots, as in customObj.0.item1 ) here lets you assign those properties to variable names . 在此处使用方括号访问属性(与点相反,如customObj.0.item1点),可以将这些属性分配给变量名 So, your final code would look like: 因此,您的最终代码如下所示:

var fn = function() {
  var list1 = [{aId: 0, name: 'item1'},
             {aId: 1, name: 'item2'},
             {aId: 2, name: 'item3'}],
    list2 = [ { id: 0, label: 'one', actions: [ 0, 1, 2 ] },
              { id: 1, label: 'two', actions: [ 0, 2 ] } ],
    customObj ={};
    for(var i=0; i<list1.length; i++) {
        for(var j=0; j<list2.length; j++){
            for(var k=0; k<list2.actions.length; k++){
                if(list1[i].aId == list2[j].actions[k]){
                    customObj[list2[j].id][list1[i].name] = true;
                }
            }
        }
    }
    return customObj;
}

It is working with this piece of code and in it loops also reduced. 它正在使用这段代码,并且循环也减少了。

var fn = function() {
   var list1 = [{aId: 0, name: 'item1'},
                {aId: 1, name: 'item2'},
                {aId: 2, name: 'item3'}],
       list2 = [{ id: 0, label: 'one', actions: [ 0, 1, 2 ] },
                { id: 1, label: 'two', actions: [ 0, 2 ] } ],
       customObj ={};
       for(var i=0; i<list2.length; i++) {
            customObj[list2[i].id] = {};
            for(var j=0; j<list1.length; j++) {
                 if(list2[i].actions.indexOf(list1[j].aId) > -1)    
                     customObj[list2[i].id][list1[j].name] = true
            }
        }
 return customObj;
}

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

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