简体   繁体   中英

Lodash transform array mix of objects and strings

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?

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

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM