1

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 ?

3
  • 1
    SO is not for what I want, its for I'm stuck here. Please google stuff, try few approaches and if you fail again, please share the attempt in the question and we will help you rectify your mistake Commented Nov 5, 2018 at 5:12
  • I dont know your background, but I suggest you make an effort to solve it yourself and if stuck post it in SO, that way you will learn. Posting requirement might get solutions but for complex requirements you may not. Commented Nov 5, 2018 at 5:31
  • @kiranvj thanks actually I tried it and I have used the for loop for this problem as well. Commented Nov 5, 2018 at 5:57

6 Answers 6

6

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);

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

2 Comments

All solutions are very similar for this question. Your solution is good and you make it one minute first but i was not copying... You have my +1
My intention was not to say you copied, obviously you have more knowledge than me. but my concern was of duplicate answers for one question... +1 from my side
3

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);

7 Comments

Hey thanks for the answer . I actually tried one more property in that object . so I tried t.questionCount and tried to add one property in this object
t.questionCount["technology"] = t.id; used this way but it is not adding that
Before concat I used this thing
would you like to add one more property to questionCount array?
yes Exactly , because it has that specific technology . So, want to add that Id if you see in my questions array of object first one, there is one Id , so want to add that in this array as well.
|
1

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)

Comments

0

This little peace of code will do it

var newData = [];
for (var i of data) {
  newData = newData.concat(i['questionCount'])
}

Comments

0

With ES6 reduce and spread, you can do something like this

const x = arr.reduce((accum, curr) => {
    let newArr = [...accum, ...curr.questionCount];
    return newArr
}, [])

Comments

0
// -- 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);

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.