2

I try to doing this object

obj={ 
    a:{ 1:"x", 2:"c"}, 
    b:{ 1:"v", 2:"b" }, 
    c:{ 4:"n", 2:"k" } 
}

to

 obj=[
    0:{group:"a" ,1:"x"},
    1:{group:"a", 2:"c"},
    2:{group:"b",1:"v"},
    3:...]
5
  • Your to object is confusing. You suggest you want an array, but that is not the proper syntax. It uses square brackets like an array, but key/val pairs like an object. One suggestion may be to be more explicit/correct in what your desired value will be Commented Oct 7, 2018 at 15:03
  • I absolutely know it's a not a free coding service. My code tries goes nothing. So I just write that what i have and what i expect. I know to rules but it's my first and last time to stretch the rules :) Commented Oct 7, 2018 at 15:24
  • @Ryoush it’s not about rules. It’s just decency. Commented Oct 8, 2018 at 7:34
  • @evolutionxbox Overreact? Just take it easy champ. Commented Oct 8, 2018 at 21:03
  • @Ryoush No worries, I’m calm. It’s always good to know where someone got to themselves. It certainly makes it easier to assist. Commented Oct 8, 2018 at 21:43

4 Answers 4

1

You can use Object.entries() to convert the object into an array. Use .reduce(), .concat() and map() to construct the new array

let obj = {
  a: {
    1: "x",
    2: "c"
  },
  b: {
    1: "v",
    2: "b"
  },
  c: {
    4: "n",
    2: "k"
  }
}


let result = Object.entries(obj)
                   .reduce((c, [k, v]) => c.concat(Object.entries(v).map(o => ({group: k,[o[0]]: o[1]}))), [])

console.log(result);

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

2 Comments

it's a great explanation.Thank you.
Happy to help :)
1

Use Object.keys to get all the keys from the object. Then use array reduce function & inside the callback function loop over the object and create a new object.

let obj = {
  a: {
    1: "x",
    2: "c"
  },
  b: {
    1: "v",
    2: "b"
  },
  c: {
    4: "n",
    2: "k"
  }
}
let m = Object.keys(obj);

let z = m.reduce(function(acc, curr) {
  if (typeof(obj[curr]) === 'object') {
    for (let keys in obj[curr]) {
      let __ob = {};
      __ob.group = curr;
      __ob[keys] = obj[curr][keys]
      acc.push(__ob)
    }

  }
  return acc;

}, [])
console.log(z)

1 Comment

its a great explanation too. Thank you so much.
1

You can iterate over your keys at both levels and use computed keys syntax as provided by ES6/ES2015:

let obj={ a:{ 1:"x", 2:"c"}, b:{ 1:"v", 2:"b" }, c:{ 4:"n", 2:"k" } }

let result = []
for (let k1 in obj){
  for (let k2 in obj[k1] ){
    result.push({group:k1,[k2]:obj[k1][k2]})
  }
}

console.log(result)

Comments

1

Using lodash, iterate the the object with _.flatMap(). In the flatMap's callback, the 1st parameter is the value ({ 1: 'x', 2: 'c' } for example), and the 2nd parameter is the key (a for example). Assign the 2nd parameter to group. Use _.toPairs() to get an array of pairs ([key, value]). Convert the pairs to object with Array.map(), and include the group:

const obj = {"a":{"1":"x","2":"c"},"b":{"1":"v","2":"b"},"c":{"2":"k","4":"n"}};

const result = _.flatMap(obj, 
  (o, group) => 
    _.toPairs(o).map(([k, v]) => ({
      group,
      [v]: k
    })));

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

1 Comment

Absolutely very happy to seeing lodash version too. Thank you so much

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.