Normalize missing value inside array of objects using nodejs

I have this data script:

var data = [
 {
  'name': 'social-button',
  'group': null
 }, {
  'name': 'social-button',
  'group': null
 }, {
  'name': 'social-button',
  'group': 'buttons'
 }, {
  'name': 'social-button',
  'group': null
 }, {
  'name': 'icon',
  'group': 'icons'
 }, {
  'name': 'other',
  'group': null
 }, {
  'name': 'icon',
  'group': null
 }
];

I would like to normalize this data:

var data = [
 {
  'name': 'social-button',
  'group': 'buttons'
 }, {
  'name': 'social-button',
  'group': 'buttons'
 }, {
  'name': 'social-button',
  'group': 'buttons'
 }, {
  'name': 'social-button',
  'group': 'buttons'
 }, {
  'name': 'icon',
  'group': 'icons'
 }, {
  'name': 'other',
  'group': null
 }, {
  'name': 'icon',
  'group': 'icons'
 }
];

Basically, I would like to ensure that every element having the same nameshould also have the same group, if only one of them has one.

Can any node module help with this?

Or maybe there is some kind of smart way to do this?

+4
source share
3 answers

You can use two loops, one for collecting values ​​in a hash table and one for assignment.

var data = [{ 'name': 'social-button', 'group': null }, { 'name': 'social-button', 'group': null }, { 'name': 'social-button', 'group': 'buttons' }, {  'name': 'social-button', 'group': null }, { 'name': 'icon', 'group': 'icons' }, { 'name': 'other', 'group': null }, { 'name': 'icon', 'group': null }],
    hash = Object.create(null);

data.forEach(function (a) {
    hash[a.name] = hash[a.name] || a.group;
});

data.forEach(function (a) {
    a.group = a.group || hash[a.name];
});

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run code
+3
source

sort() , , map() . Object.assign() , .

var data = [{"name":"social-button","group":null},{"name":"social-button","group":null},{"name":"social-button","group":"buttons"},{"name":"social-button","group":null},{"name":"icon","group":"icons"},{"name":"other","group":null},{"name":"icon","group":null}]

var result = data.sort((a, b) => b.group != null).map(function(o) {
  if (!this[o.name]) {
    this[o.name] = o.group
    return o
  } else {
    var obj = Object.assign({}, o);
    obj.group = this[o.name];
    return obj
  }
}, {})

console.log(result)
0

You can do this in one run using Array.prototype.reduceand hash table- see the demo below:

var data=[{'name':'social-button','group':null},{'name':'social-button','group':null},{'name':'social-button','group':'buttons'},{'name':'social-button','group':null},{'name':'icon','group':'icons'},{'name':'other','group':null},{'name':'icon','group':null}];

var result = data.reduce((function(hash){
  return function(p,c) {
    hash[c.name] = hash[c.name] || c;
    if(c.group)
      hash[c.name].group = c.group;
    p.push(hash[c.name]);
    return p;
  };
})(Object.create(null)), []);

console.log(JSON.stringify(result));
.as-console-wrapper{top:0;max-height:100%!important;}
Run code
0
source

Source: https://habr.com/ru/post/1666000/


All Articles