javascript - How to convert array of Object into keyvalue pair typescript - Stack Overflow

Currently I'm getting data in the format below:arr=[0: {abc:1},1: {efg:2},2: {hij:3}]I need it in

Currently I'm getting data in the format below:

arr=[
       0: {abc:1},
       1: {efg:2},
       2: {hij:3}
    ]

I need it in below format:

arr=[
      {name:'abc', value:1},
      {name:'efg', value:2},
      {name:'hij', value:3}]

Currently I'm getting data in the format below:

arr=[
       0: {abc:1},
       1: {efg:2},
       2: {hij:3}
    ]

I need it in below format:

arr=[
      {name:'abc', value:1},
      {name:'efg', value:2},
      {name:'hij', value:3}]
Share Improve this question edited Jun 19, 2018 at 21:25 Mike Williamson 3,28016 gold badges77 silver badges119 bronze badges asked Jun 19, 2018 at 21:17 user5553165user5553165 3
  • 1 so start looping and convert it. developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – epascarello Commented Jun 19, 2018 at 21:19
  • This question is lacking content and understanding. It needs to be clarified before it can be answered. Think more in terms of WHAT you want to do, not what should e out of it. Do you want to access the values in the objects but you don't know the keys? – Robert Mennell Commented Jun 19, 2018 at 21:21
  • 2 Also another question: What happens if you have more than one key and value per object? – Robert Mennell Commented Jun 19, 2018 at 21:34
Add a ment  | 

2 Answers 2

Reset to default 4

Assuming an arr has a following structure, it's only a matter of mapping through it and separating key and value:

var arr = [ 
    {abc:1},
    {efg:2},
    {hij:3}
]

var result = arr.map(o => {
    var k = Object.keys(o)[0];
    return {
        name: k,
        value: o[k]
    };
});

console.log(result);
var arr = [  {abc:1}, {efg:2},{hij:3}],
var arr1=[];

      _.forEach(arr, function (val, key) {
        _.forIn(val, function (val, key) {
          arr1.push({ name: key, value: val });
        })
      });
    console.log(arr1);

    });**Implemented using loadash**

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742253702a4409628.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信