1

I need to get array of objects from array of strings. For examaple:

var arr = ["1005", "1005", "1005", "1006", "1006", "1006", "1007", "1007"];



   var result = arr.reduce((iss, index) => {
  iss[index] = (iss[index] || 0) + 1;
    return iss 
}, {});

and the result would be

{1005: 3, 1006: 3, 1007: 2}

So is there a way to get next output:

[{"1005":3},{"1006":3},{"1007":2}]
1
  • 1
    Why? There is no advantage of that structure. Commented Feb 1, 2018 at 12:54

2 Answers 2

3

If you really want that:

 result = Object.entries(result).map(([key, value]) => ({[key]: value}));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! All of you help me a lot!
0

You can extend result by iterating its keys

result = Object.keys ( result ).map( s => ({ [s] : result[s] }) );

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.