3

I have an array that contains a mix of objects and strings. I need to transform the array into another object array.

The input array:

[
  {"text": "Address"},
  {"text": "NewTag"},
  {"text": "Tag"},
  "Address",
  "Name",
  "Profile",
  {"text": "Name"},
]

The out array should like this:

[
  {"Tag": "Address", Count: 2},
  {"Tag": "Name", Count: 2},
  {"Tag": "NewTag", Count: 1},
  {"Tag": "Profile", Count: 1},
  {"Tag": "Tag", Count: 1},
]

Here is my code (it looks stupid):

var tags = [], tansformedTags=[];   
for (var i = 0; i < input.length; i++) {
  if (_.isObject(input[i])) {
    tags.push(input[i]['text']);
  } else {
    tags.push(input[i]);
  }
}
tags = _.countBy(tags, _.identity);
for (var property in tags) {
  if (!tags.hasOwnProperty(property)) {
    continue;
  }
  tansformedTags.push({ "Tag": property, "Count": tags[property] });
}
return _.sortByOrder(tansformedTags, 'Tag');

I want to know if there is a better and more elegant way to perform this operation?

2 Answers 2

2

By using map() and countBy():

_(arr)
    .map(function(item) {
        return _.get(item, 'text', item);
    })
    .countBy()
    .map(function(value, key) {
        return { Text: key, Count: value };
    })
    .value();
Sign up to request clarification or add additional context in comments.

Comments

2

You can use Object.create(null) to create a hash table where you can count the properties in your array, then get its properties with Object.keys, and use map to build your objects.

var count = Object.create(null);
myArray.forEach(function(item) {
  var prop = Object(item) === item ? item.text : item;
  count[prop] = (count[prop] || 0) + 1;
});
Object.keys(count).sort().map(function(key) {
  return {Tag: key, Count: count[key]};
});

2 Comments

The OP is using lodash. Maybe he/she needs to write code that is compatible with (really) old browsers.
I like this solution too. Code is concise and clear.

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.