有没有一种方法可以遍历数组并将值添加到对象调用的末尾示例experienceObject [property] .columns [i]

时间:2019-07-12 17:12:39

标签: javascript arrays json object

 const columns =["c","f","e","d","a","b","g"];
 const  experienceObject = {"Username":"participant1","Email":"P1@email.com","a":true,"b":true},{"Username":"participant2","Email":"P2@email.com","c":true,"d":true};

 for (property in experienceObject) {
   var Username = experienceObject[property].Username;
   var Email = experienceObject[property].Email;

   for (propertyCol in columns) { 
     function loopID(a){
       var aL = columns.length;
       var output = '';


       for(var i = 0; i < aL; i++ ){
         output += columns[i]+'('+experienceObject[property].columns[i]+')';
       }
       return output;
     }

   }
   getFunction = loopID();

1 个答案:

答案 0 :(得分:0)

基于类似以下内容的评论

var results;

for (property in experienceObject) {
  if (columns.includes(property)) 
  {
    results[property] = experienceObject[property];
  }
}
return results;

下面的旧答案


也许您打算这样做

const  experienceObject = 
   {"Username":"participant1","Email":"P1@email.com","a":true,"b":true},
   {"Username":"participant2","Email":"P2@email.com","c":true,"d":true};

成为这个

const  experienceObject = 
  [{"Username":"participant1","Email":"P1@email.com","a":true,"b":true},
   {"Username":"participant2","Email":"P2@email.com","c":true,"d":true}];

因此它是一个数组,而不是该行上的第二个对象。

这意味着您也想要吗?

for (property in experienceObject[0]) {