How to convert an array with one object and multiple keys into an array of multiple objects using those keys and their values?

naru-magi

I have an array like so with a single object inside:

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:

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. Those pairs are then put into their own object inside the array.

Note that "category": "None" is not its own object in the array since "None" is non-numerical.

It's also important to note that there could be many key-value pairs, so it's not just limited to the items above (e.g., "ARFE": 553.5, etc.)

What I have so far:

I know you can separate a single object into multiple objects:

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:

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?

Cerbrus

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:

const result = Object
    .entries(data)
    .filter(([ key ]) => key !== 'category')
    .map(([key, value]) => ({ name: key, value }));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sum array objects by multiple keys

Using jq, convert array of objects to object with named keys

Convert array values to object keys

PHP, array of objects, how can I use one set of object values as the array keys

convert object keys and values to an array of objects

Convert array of objects into an object with only values (no index or keys)

Convert Nested Object into Array of Objects and remove keys

PHP array: how to find all keys in array by passing multiple values?

Convert String Array to Array Object with keys and values

How to sort array containing objects by those object keys

Removing elements from an array of objects based on duplicate values of multiple keys

Merge values of multiple same keys in array of objects with jQuery (JavaScript)

How to include multiple keys and values in array

How to build new object using keys from array of keys and values from another array of objects

How to convert an array of keys to an object?

Sum array of objects values by multiple keys in array

filter array of objects based on separate object values by keys using reactjs

Convert an object into an array of objects base on a substring in the keys

Convert a array of objects to a multiple keys of object

How to multiply objects inside an array and for each object, taking one or multiple keys and decrease their values?

How i can get from an array with multiple object, same array but with only specific keys and values?

How to sum values of equivalent keys in an array of objects where there can be multiple, newly generated keys?

How to return a array from array of object based on multiple keys

Create array of multiple objects with different structure from array of objects with duplicate keys using Object.assign() in JavaScript

How to convert an object to array of multiple objects in javascript

Group Array of Object by multiple keys

How to filter from an array of objects using multiple filter words and multiple keys

Copy values from multiple keys from array of object

How to group & sum by multiple keys in an object array?