[英]How to convert an array with one object and multiple keys into an array of multiple objects using those keys and their values?
I have an array like so with a single object inside:我有一个像这样的数组,里面有一个 object:
FirstArray = [{
"category": "None",
"ARFE": 553.5,
"BV": 900,
"RF rfeer": 0,
.....
}]
I want to convert it so that every key-value pair (where the value is a number) in the object is in its own object like the following:我想转换它,以便 object 中的每个键值对(其中值是一个数字)都在它自己的 object 中,如下所示:
NewArray = [{
name: "ARFE",
value: 553.05
}, {
name: "BV",
value: 900
}, {
name: "RF rfeer",
value: 0
}, .....]
Here, each key was assigned a new key called name , and the value for the original key was assigned a new key called value .在这里,每个键都被分配了一个名为name的新键,而原始键的值被分配了一个名为value的新键。 Those pairs are then put into their own object inside the array.
然后将这些对放入阵列内自己的 object 中。
Note that "category": "None" is not its own object in the array since "None" is non-numerical.请注意, “类别”:“无”在数组中不是它自己的 object,因为“无”是非数字的。
It's also important to note that there could be many key-value pairs, so it's not just limited to the items above (eg, "ARFE": 553.5, etc.)还需要注意的是,可能有许多键值对,因此不仅限于上述项目(例如,“ARFE”:553.5 等)
What I have so far:到目前为止我所拥有的:
I know you can separate a single object into multiple objects:我知道您可以将单个 object 分成多个对象:
NewArray = Object.entries(FirstArray).reduce((prev, [og, nw]) => {
let [name, value] = og.match(/\D+|\d+$/g)
prev[value] = { ...(prev[value] || {}), [name]: nw }
return prev;
}, {})
I also know how that you can create a new object with new keys like so:我也知道如何使用新密钥创建一个新的 object,如下所示:
NewArray = Object.assign(
...Object.entries(FirstArray).map(([key, value]) => ({ [key]: name }))
);
However, I'm having trouble putting everything together.但是,我无法将所有内容放在一起。 How would I be able to achieve NewArray from FirstArray?
我如何能够从 FirstArray 实现 NewArray?
You were pretty close.你非常接近。 All you needed to do is specify the name:
您需要做的就是指定名称:
const data = { "category": "None", "ARFE": 553.5, "BV": 900, "RF rfeer": 0 }; const result = Object.entries(data).filter(([_, value]) => typeof value === 'number').map(([key, value]) => ({ name: key, value })); console.log(result);
Also, if you don't want { "name": "category", "value": "None" }
to be included in the result, you can just filter it:此外,如果您不希望
{ "name": "category", "value": "None" }
包含在结果中,您可以过滤它:
const result = Object
.entries(data)
.filter(([ key ]) => key !== 'category')
.map(([key, value]) => ({ name: key, value }));
Object.entries
on array has no sense at all, use it on the object Object.entries
on array 根本没有意义,在 object 上使用它
const FirstArray = [{ "category": "None", "ARFE": 553.5, "BV": 900, "RF rfeer": 0, }] const newObject = Object.entries(FirstArray[0]).reduce((array, [key, value]) => { return [...array, { name: key, value }] }, []) console.log(newObject)
reduce
is not the right way to go. reduce
不是 go 的正确方法。 Simply use map
:只需使用
map
:
Object.entries(FirstArray[0])
.filter(x => !isNaN(x[1])) // filter out non-numeric values
.map(([name, value]) => ({name, value}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.