映射数组的值

时间:2017-05-09 01:54:25

标签: javascript json ecmascript-6 underscore.js lodash

Json的My Array如下所示

[
  {Priority: low,
Status: received
Creator: ABC
},
 {Priority: high,
Status: received
Creator: DEF
}
  ]

我需要映射优先级字段的值,优先级字段必须映射到颜色,值如下所示

[
  {color: Orange,
Status: received
Creator: ABC
},
 {color: red,
Status: received
Creator: DEF
}

  ]

4 个答案:

答案 0 :(得分:0)

您不需要lodash或下划线,Array.prototype.map可以提供帮助:



let originArray = [
  {Priority: 'low',
    Status: 'received',
    Creator: 'ABC'
  },
  {Priority: 'high',
    Status: 'received',
    Creator: 'DEF'
  }
];

function getColorFromPriority(priority) {
  if (priority === 'low') {
    return 'yellow';
  } else if (priority === 'high') {
    return 'red';
  }
}

let resultArray = originArray.map(function(o) {
  o.color = getColorFromPriority(o.Priority);
  delete o.Priority;
  return o;
});

console.log(resultArray);




答案 1 :(得分:0)

只使用JavaScript,您可以使用Array.prototype.map()Object.keys(),您可以遍历数组并返回另一个包含自定义对象的数组。

这就是你的代码:

var filteredResults = data.map(function(item) {
  var obj = {};
  Object.keys(item).forEach(function(k) {
    if (k !== 'Priority' && item.hasOwnProperty(k)) {
      obj[k] = item[k]
    } else if (k === 'Priority' && item.hasOwnProperty(k)) {
      if (item[k] === 'low') obj['color'] = 'Orange';
      else if (item[k] === 'high') obj['color'] = 'Red';
    }
  });
  return obj;
});

<强>演示:

这是一个有效的演示片段:

var data = [{
    Priority: 'low',
    Status: 'received',
    Creator: 'ABC'
  },
  {
    Priority: 'high',
    Status: 'received',
    Creator: 'DEF'
  }
];

var filteredResults = data.map(function(item) {
  var obj = {};
  Object.keys(item).forEach(function(k) {
    if (k !== "Priority" && item.hasOwnProperty(k)) {
      obj[k] = item[k]
    } else if (k === 'Priority') {
      if (item[k] === 'low') obj['color'] = 'Orange';
      else if (item[k] === 'high') obj['color'] = 'Red';
    }
  });
  return obj;
});

console.log(filteredResults);

改进:

您可以看到我在这里使用了两个if块来测试Priority级别:

if (item[k] === 'low') obj['color'] = 'Orange';
else if (item[k] === 'high') obj['color'] = 'Red';

如果您有更多Priority级别,最好使用自定义object来存储这些级别及其各自的颜色,例如:

var levels = {"low" : "Orange", "high": "Red"}

然后只需使用这些级别keys来获取相应的Color,就像这样:

obj['color'] = levels[item[k]];

答案 2 :(得分:0)

你可以使用Array#map和一个存储优先级和颜色之间映射的对象

如果您需要更多优先级颜色 - 将它们添加到mappingColors对象中,其余代码保持不变

const data = [{
    Priority: 'low',
    Status: 'received',
    Creator: 'ABC'
  },
  {
    Priority: 'high',
    Status: 'received',
    Creator: 'DEF'
  }
];

const mappingColors = {
  low: 'orange',
  high: 'red',
}

let finalArray = data.map(d => (d.Color = mappingColors[d.Priority],delete d.Priority, d));
console.log(finalArray);

答案 3 :(得分:0)

正如其他答案所述,这可以在没有Lodash的情况下完成。毫无疑问地添加Lodash(带ES6)选项:

let arr = [  {Priority: 'low',Status: 'received', Creator: 'ABC'}, {Priority: 'high',Status: 'received', Creator: 'DEF'}  ];

let colors = new Map([['low', 'Orange'],['high', 'Red']]);
let res= arr.map(o=> _.assign({color:colors.get(o.Priority)},_.omit(o,'Priority')));

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