本文介绍了将对象及其属性数组转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了几种map函数,但是找不到找到我想要的东西的正确方法。就是这种情况:
I tried several of the map functions but could not find a proper way to get what I want. Here is the case:
Object {Results:Array[3]} Results:Array[3] [0-2] 0:Object id=null name: "Rick" upper:"0.67" 1:Object id="00379321" name:null upper:"0.46" 2:Object id="00323113" name:null upper:null我希望我的最终结果看起来像这样。我希望删除所有空值,并将所有条目这样绑在一个对象中。
I want my final result to look like this. I wanted all null values to be removed and all the entries tied up like this in an object.
var finalResult = ["Rick","0.67","00379321","0.46","00323113"];如何获得此结果?
推荐答案我建议对键使用固定的数组,因为对象的属性没有顺序并且顺序是相关的。
I suggest to use a fixed array for the keys, because the properties of an object have no order and the order is relevant.
var data = [{ id: null, name: "Rick", upper: "0.67" }, { id: "00379321", name: null, upper: "0.46" }, { id: "00323113", name: null, upper: null }], result = []; data.forEach(function (a) { ['id', 'name', 'upper'].forEach(function (k) { if (a[k] !== null) { result.push(a[k]); } }); }); console.log(result);
更多推荐
将对象及其属性数组转换为数组
发布评论