0

I have a object like this:

{
            "ABC":{"minValue":0,"maxValue":5},
            "XYZ":{"minValue":0,"maxValue":5},
            "PQR":{"minValue":0,"maxValue":5},
            "overall":{"minValue":0,"maxValue":5}
}

I am trying to make a simple array like this

[
            ["type":"ABC","minValue":0,"maxValue":5],
            ["type":"XYZ","minValue":0,"maxValue":5],
            ["type":"PQR","minValue":0,"maxValue":5],
            ["type":"overall","minValue":0,"maxValue":5]
]

not getting idea how to get it.

Here is my attempt:

var s = scoreFilters;  //my json object
    var out = Object.keys(s).map(function(data){
        console.log(data)
        return [["type":data,"minValue":data.minValue,"maxValue":data.maxValue]];
    });
4
  • 3
    Your expected output is not valid JavaScript. Commented Mar 19, 2019 at 6:06
  • my expected out put is array. i think its possible Commented Mar 19, 2019 at 6:07
  • 2
    I think ["type":"ABC","minValue":0,"maxValue":5] should be {"type":"ABC","minValue":0,"maxValue":5} Commented Mar 19, 2019 at 6:08
  • Yes Senal is right. You can't have a array with key value pairs. Commented Mar 19, 2019 at 6:16

3 Answers 3

2

Assuming you want an array of objects for your output, you can .map the Object.entries of your input:

const obj = {
  "ABC":{"minValue":0,"maxValue":5},
  "XYZ":{"minValue":0,"maxValue":5},
  "PQR":{"minValue":0,"maxValue":5},
  "overall":{"minValue":0,"maxValue":5}
};
const arr = Object.entries(obj).map(([type, { minValue, maxValue }]) => ({
  type,
  minValue,
  maxValue
}));
console.log(arr);

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

Comments

2

You could use Object.entries, map and spread syntax like this:

let input = {"ABC":{"minValue":0,"maxValue":5},"XYZ":{"minValue":0,"maxValue":5},"PQR":{"minValue":0,"maxValue":5},"overall":{"minValue":0,"maxValue":5}}

let output = Object.entries(input).map(([type, values]) => ({ type, ...values}))

console.log(output)

Comments

1

You can use Object.entries() and .map() to get an array of objects:

const data = {
  "ABC": {"minValue": 0,"maxValue": 5},
  "XYZ": {"minValue": 0,"maxValue": 5},
  "PQR": {"minValue": 0,"maxValue": 5},
  "overall": {"minValue": 0,"maxValue": 5}
};

const result = Object.entries(data)
                     .map(([ k, v ]) => Object.assign({}, {type: k},  v));
                     
console.log(result);
.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.