I am new to reactjs
and javascript as well.
Here, I have an array of object which is like ,
[
{
"id": "CSS",
"questionCount": [
{
"level": "TOUGH",
"type": "NON_CODE",
"count": "1"
}
]
},
{
"id": "Backbone",
"questionCount": [
{
"level": "TOUGH",
"type": "CODE",
"count": "2"
},
{
"level": "TOUGH",
"type": "NON_CODE",
"count": "5"
},
{
"level": "MEDIUM",
"type": "NON_CODE",
"count": "7"
},
{
"level": "EASY",
"type": "NON_CODE",
"count": "6"
}
]
},
]
Now, here what I want is to have an array of object which will have all the objects that are present in the questionCount
array. so, it will be like ,
[ {
"level": "TOUGH",
"type": "NON_CODE",
"count": "1"
}, {
"level": "TOUGH",
"type": "CODE",
"count": "2"
},
{
"level": "TOUGH",
"type": "NON_CODE",
"count": "5"
},
{
"level": "MEDIUM",
"type": "NON_CODE",
"count": "7"
},
{
"level": "EASY",
"type": "NON_CODE",
"count": "6"
} ]
So, can any one help me with this ?
You can use Array.prototype.reduce() with Array.prototype.concat() :
const temp = [{"id": "CSS","questionCount": [{"level": "TOUGH","type": "NON_CODE","count": "1"}]},{"id": "Backbone","questionCount": [{"level": "TOUGH","type": "CODE","count": "2"},{"level": "TOUGH","type": "NON_CODE","count": "5"},{"level": "MEDIUM","type": "NON_CODE","count": "7"},{"level": "EASY","type": "NON_CODE","count": "6"}]},]; const result = temp.reduce((a, c) => a.concat(c.questionCount), []); console.log(result);
You can iterate through each element and concatenate it with result. Try with the followings:
var jsonObject = [{"id": "CSS","questionCount": [{"level": "TOUGH","type": "NON_CODE","count": "1"}]},{"id": "Backbone","questionCount": [{"level": "TOUGH","type": "CODE","count": "2"},{"level": "TOUGH","type": "NON_CODE","count": "5"},{"level": "MEDIUM","type": "NON_CODE","count": "7"},{"level": "EASY","type": "NON_CODE","count": "6"}]}] var result = []; for(var t of jsonObject){ if(t.questionCount){ //As t.questionCount is an array, we need to add property to each of the element present in that array t.questionCount.forEach(obj => obj.Id = t.id); result = result.concat(t.questionCount); } } console.log(result);
Lets say temp is given array, then you can can array of questionCount using following code:-
finalArr=[]
for(var i=0;i<temp.length;i++)
finalArray.push(temp[i].questionCount)
This little peace of code will do it
var newData = [];
for (var i of data) {
newData = newData.concat(i['questionCount'])
}
With ES6 reduce and spread, you can do something like this
const x = arr.reduce((accum, curr) => { let newArr = [...accum, ...curr.questionCount]; return newArr }, [])
// -- Use the swiss-knife-tool :-) (Lodash)
// -- Taking x as your array in the question,
// -- you can use lodash to map specific attribute
// -- and then flatten the output array.
const y = _
.chain(x)
.map(item => item.questionCount)
.flatten()
.value();
console.log(y);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.