将对象及其属性数组转换为数组

编程入门 行业动态 更新时间:2024-10-25 00:24:58
本文介绍了将对象及其属性数组转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试了几种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);

更多推荐

将对象及其属性数组转换为数组

本文发布于:2023-11-30 12:08:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649890.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   属性   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!