如何从对象数组创建数组

时间:2016-09-20 16:45:54

标签: javascript arrays arraylist

鉴于以下内容:

var example = [{
  "logged": {
    "status": 0,
    "status_name": "Logged",
    "hours": 23 // to the hours array
  },
  "pending": {
    "status": 3,
    "status_name": "Pending",
    "contribution": {
      "hours": 0,
      "count": 0
    },
    "verification": {
      "hours": 6, // to the hours array
      "count": 1
    }
  },
  "denied": {
    "status": 4,
    "status_name": "Denied",
    "contribution": {
      "hours": 0,
      "count": 0
    },
    "verification": {
      "hours": 0, // to the hours array
      "count": 0
    }
  },
  "approved": {
    "status": 2,
    "status_name": "Approved",
    "contribution": {
      "hours": 4.5,
      "count": 1
    },
    "verification": {
      "hours": 0, // to the hours array
      "count": 0
    }
  }
}];

我如何构建两个这样的数组:

var statusNames = ["Logged", "Pending", "Denied", "Approved"];
var hours = [23, 6, 0, 0];

如果解决方案中的值,我可以推断如何得到其余部分。

5 个答案:

答案 0 :(得分:2)

您可以遍历数组和键,检查属性是否存在并将所需部分包含在数组中。

var example = [{ "logged": { "status": 0, "status_name": "Logged", "hours": 23 }, "pending": { "status": 3, "status_name": "Pending", "contribution": { "hours": 0, "count": 0 }, "verification": { "hours": 6, "count": 1 } }, "denied": { "status": 4, "status_name": "Denied", "contribution": { "hours": 0, "count": 0 }, "verification": { "hours": 0, "count": 0 } }, "approved": { "status": 2, "status_name": "Approved", "contribution": { "hours": 4.5, "count": 1 }, "verification": { "hours": 0, "count": 0 } } }],
    statusNames = [],
    hours = [];

example.forEach(function (o) {
    Object.keys(o).forEach(function (k) {
        if ('hours' in o[k]) {
            statusNames.push(k);
            hours.push(o[k].hours);
            return;
        }
        if ('verification' in o[k]) {
            statusNames.push(k);
            hours.push(o[k].verification.hours);
        }
    });
});
console.log(statusNames);
console.log(hours);

答案 1 :(得分:1)

使用Object.keys方法获取所有对象属性名称数组,然后使用Array#map方法生成结果数组。

// array for storing the hours value
var hours = [];
// genarte the statusName array
var statusNames = Object.keys(example[0]) // get object keys
            // iterate over key value          
            .map(function(k) {
                // get the value of hour and push it to hours array
                // get `hours` property if defined or get hours property
                // of nested object verification or push 0
                hours.push(example[0][k].hours || example[0][k].verification.hours || 0); 
                // get the status name and return
                return example[0][k].status_name;
            })



var example = [{
  "logged": {
    "status": 0,
    "status_name": "Logged",
    "hours": 23 // to the hours array
  },
  "pending": {
    "status": 3,
    "status_name": "Pending",
    "contribution": {
      "hours": 0,
      "count": 0
    },
    "verification": {
      "hours": 6, // to the hours array
      "count": 1
    }
  },
  "denied": {
    "status": 4,
    "status_name": "Denied",
    "contribution": {
      "hours": 0,
      "count": 0
    },
    "verification": {
      "hours": 0, // to the hours array
      "count": 0
    }
  },
  "approved": {
    "status": 2,
    "status_name": "Approved",
    "contribution": {
      "hours": 4.5,
      "count": 1
    },
    "verification": {
      "hours": 0, // to the hours array
      "count": 0
    }
  }
}];

var hours = [];
var statusNames = Object.keys(example[0]).map(function(k) {
  hours.push(example[0][k].hours || example[0][k].verification.hours || 0);
  return example[0][k].status_name;
})

console.log(hours, statusNames)




答案 2 :(得分:1)

您必须使用Array.prototype.reduce来过滤所需的信息。

var example = [{
  "logged": {
    "status": 0,
    "status_name": "Logged",
    "hours": 23 // to the hours array
  },
  "pending": {
    "status": 3,
    "status_name": "Pending",
    "contribution": {
      "hours": 0,
      "count": 0
    },
    "verification": {
      "hours": 6, // to the hours array
      "count": 1
    }
  },
  "denied": {
    "status": 4,
    "status_name": "Denied",
    "contribution": {
      "hours": 0,
      "count": 0
    },
    "verification": {
      "hours": 0, // to the hours array
      "count": 0
    }
  },
  "approved": {
    "status": 2,
    "status_name": "Approved",
    "contribution": {
      "hours": 4.5,
      "count": 1
    },
    "verification": {
      "hours": 0, // to the hours array
      "count": 0
    }
  }
}]

var statusNames = example.reduce(function(prevVal, currVal) {
  for(var key in currVal) {
    if(!Object.prototype.hasOwnProperty.call(currVal, key)) continue

    if(currVal[key].status_name) {
      prevVal.push(currVal[key].status_name)
    }
   
  }
  return prevVal
}, [])


console.log(statusNames)

答案 3 :(得分:0)

这应该有用。

// get all of the objects keys
var statusNames = Object.keys(example);
var hours = [];

// get all of the hours
example.forEach( function(item, idx){
    idx === 0 ? hours.push(item.hours) : false; // because the hours in your first object is not nested.
    idx > 0 ? hours.push(item.verification.hours) : false; // gets all of the nested hours.
});

答案 4 :(得分:0)

使用$ g++ prime_calc.cpp -o prime_calc $ ./prime_calc 3 1500 12553 15000 163841 15001 That value is too large, try a number <= 15000. 迭代每个键。

然后按下各个数组中的值

for (var key in example[0])

&#13;
&#13;
statusNames.push(key);
hours.push(example[0][key]["hours"] || example[0][key]["verification"]["hours"] );
&#13;
&#13;
&#13;