1

I currently have a function which returns this array:

var myArray = [
    { Sepal_Width: { Average_Sepal_Width: series => series.average() } },
    { Sepal_Width: { Sum_Sepal_Width: series => series.sum() } },
    { Petal_Length: { Average_Petal_Length: series => series.average() } }
]

But I need the output to look like this:

Sepal_Width: { 
     Average_Sepal_Width: series => series.average(),
     Sum_Sepal_Width: series => series.sum() 
},
Petal_Length: { 
     Average_Petal_Length: series => series.average() 
}

I'm just wondering if this is even possible, as I need to account for the user adding additional lines to the original array like { Petal_Length: Sum_Petal_Length: series => series.sum() }} and have this automatically go within the Petal_Length: {}.

Sepal_Width: { 
     Average_Sepal_Width: series => series.average(),
     Sum_Sepal_Width: series => series.sum() 
},
Petal_Length: { 
     Average_Petal_Length: series => series.average() },
     Sum_Petal_Length: series => series.sum() }
}
2
  • What datatype are those values? series => series.average(). Is it a function? (myArray) Commented Jul 17, 2019 at 18:39
  • 1
    Why? In any case, there's no trivial way to get it formatted like this, especially if you want to keep the function source code available. Commented Jul 17, 2019 at 18:40

1 Answer 1

3

You could get the first key and assign the values to the given key at the result object.

var array = [{ Sepal_Width: { Average_Sepal_Width: series => series.average() } }, { Sepal_Width: { Sum_Sepal_Width: series => series.sum() } }, { Petal_Length: { Average_Petal_Length: series => series.average() } }],
    result = array.reduce((r, o) => {
        Object.entries(o).forEach(([k, v]) => Object.assign(r[k] = r[k] || {}, v));
        return r;
    }, {});

console.log(result);

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

4 Comments

This was incredibly helpful but I'm trying to understand what the code is doing in words, haha -- I think that Object.entries turns the object into k,v pairs within an array [[k,v],[k,v]], but I'm confused what's going on with the Object.assign to get these values into the format I want. Thank you again for your help!
right. Object.entries returns an array of key/value arrays, the later Object.assign takes a target object and assigns the following object to it, after taking a property with the name k or it creates a new object for it.
...So it turns out each of my objects within the array are actually being returned as a string (UGH) I thought JSON.parse would help me but to no avail. Any ideas? I've reproduced my issue here repl.it/@MayaJaffe/string-manipulation Thank you again for your help, I'm learning so much!
it is only possible to use eval and generate an object from the string. it is not possible to parse it as JSON, because it does not comply with the specs.

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.