0

Are the steps I'm taking to solve this problem correct?

I'm working on turning a data structure of an array of arrays such as

this.arrayofAnimalsAndValues = [
  [ "Ant", 1287, 12956],
  [ "Lion", 2574, 25826],
  [ "Bear", 3861, 38696],
  .....
]

into this

this.jsonOfAnimalsAndValues = [
  {category: "Ant", value_1: 1287, value_2:12956},
  {category: "Lion", value_1: 2574, value_2:25826},
  {category: "Bear", value_1: 3861, value_2:38696},
  .....
]

where the first item in the array is always assigned to 'category' in the array object and the following items assigned to value_# depending on their order in the array. So the 2nd array item would have key value_1 and so on. For example, for 1 nested array:

[[ "Ant", 5148, 51566]] to =>
[{category: "Ant", value_1: "5148", value_2: 51566}]

I've created a hardcoded way to achieve this however I'm trying to make it dynamic:

'hardcoded' way:

this.variableOfKeys = ["value_1", "value_2", "value_3", ......]

this.jsonOfAnimalsAndValues = this.arrayofAnimalsAndValues(function(x) { 
    return { 
       category: x[0], 
       no1: x[1],
       no2: x[2],
       .....
    }; 
});

where I just hardcode the keys and their values (values using their index).

My attempt to make it dynamic:

      this.variableOfKeys.forEach(element => {
        this.jsonOfAnimalsAndValues = this.arrayofAnimalsAndValues.map(function(x) { 
          for (var i = 0; i<=this.arrayOfValuesToUseAsKeys.length; ++i) {
            return { 
              category: x[0], 
              element: x[i+1],
            }; 
          }
        });
      });

where my logic is that for each item in

this.variableOfKeys = ["value_1", "value_2", "value_3", ......],

I created this.jsonOfAnimalsAndValues such that the first item (item with the 0th index) in the array this.arrayofAnimalsAndValues is assigned to the key category and the following items (depending on their index) are assigned to the values in this.variableOfKeys in order starting from no1, then no2 etc.

However, I don't think this is written correctly and I keep getting this error:

"TypeError: Cannot read property 'variableOfKeys' of undefined"

Can I ask how it might be written incorrectly and so how I might be able to create this.jsonOfAnimalsAndValues from this.arrayofAnimalsAndValues?

2 Answers 2

2

You can map each subarray to an array of entries, then turn it into an object to return with Object.fromEntries:

const arrayofAnimalsAndValues = [
  [ "Ant", 1287, 12956],
  [ "Lion", 2574, 25826],
  [ "Bear", 3861, 38696],
];
const output = arrayofAnimalsAndValues.map(
  ([category, ...rest]) => Object.fromEntries([
    ['category', category],
    ...rest.map((value, i) => ['value_' + (i + 1), value])
  ])
);
console.log(output);

Sign up to request clarification or add additional context in comments.

Comments

1

Given an array of the keys you can map() the main array and use reduce() on each subarray to generate each object

const arr=[["Ant",1287,12956],["Lion",2574,25826],["Bear",3861,38696]],
      keys = ['category','value_1','value_2'];

const res = arr.map(e => e.reduce((a,c,i) => (a[keys[i]] = c, a),{}))

console.log(res)
.as-console-wrapper {   max-height: 100%!important;top:0}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.