将嵌套对象数组转换为类似JSON的对象?

时间:2017-01-23 22:15:36

标签: javascript arrays object dictionary nested

我看了很多地方,但找不到类似的东西。我需要像这样转换array嵌套objects

var currentArr = [ 
  { name: 'Michael',   
    children: ['Lara'] 
  },
  { name: 'Susy',    
    children: ['Mike','Ana','Mary']
  },
  { name: 'Bruno', 
    children: ['Clark']
  }
]

进入这样的JSON对象:

var result = [
    { 'name' : 'Michael', 
      'children': [
            { 'name' : 'Lara' }
    },
    { 'name' : 'Susy', 
      'children': [
            { 'name' : 'Mike' },
            { 'name' : 'Ana' },
            { 'name' : 'Mary' }
      ]
    },
    { 'name' : 'Bruno', 
       'children': [
            { 'name' : 'Clark' }
        ]
    }
]

我已尝试使用reducemap嵌套,但我无法通过此方式来构建我想要的内容。特别是在循环来自children的字符串数组时,我需要以name作为键返回映射结果。

它需要嵌套为resultcurrentArr将是入口点。

5 个答案:

答案 0 :(得分:0)

您只需映射每个SELECT u.id, u.uuid, ARRAY( SELECT row_to_json(b.*) FROM ( SELECT prj.id FROM "Projects" prj INNER JOIN "Permissions" prm ON prj.id = prm.project_id WHERE prm.user_id = u.id ) b ) as projects, ARRAY( SELECT row_to_json(c.*) FROM ( SELECT cmp.id, cmp.project_id FROM "Components" cmp INNER JOIN "Permissions" prm ON cmp.project_id = prm.project_id WHERE prm.user_id = u.id ) c ) as components FROM "Users" u WHERE u.uuid = ? 数组并返回一个新对象。



For i = 2 To lrow

Cells(i, 1).Activate
x = Application.VLookup(ActiveCell.Offset(0, 0).Value, Worksheets("MiscDefinitions").Range("G1:H11"), 2, False)
    If x <> Cells(i, 4).Value Then
        Cells(i, 4).Interior.Color = RGB(255, 0, 0)
    Else
    End If
Next i
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这样就不会修改原始数组:

var result = current.map(function (parent) {
    return {
        name: parent.name,
        children: parent.children.map(function (name) {
            return { name: name };
        })
    };
});

// if you want a JSON string representing the result object
var resultJSON = JSON.stringify(result);

另外,正如其他人所指出的那样,原始代码段中的resultObj不是有效对象。也许你的意思是它是一个阵列?

答案 2 :(得分:0)

如果可以接受修改当前阵列,则此ES2015代码就足够了:

currentArr.forEach(function(p) {
    p.children = p.children.map(name => ({ name }));
});

箭头函数的返回值利用了新的ES2015语法,该语法表示没有值的对象文字中的裸键获取该名称变量的当前值,即它与写{ "name": name }

必须使用额外的括号来阻止大括号被解释为函数体而不是对象文字。

答案 3 :(得分:0)

我会这样做:

&#13;
&#13;
var currentArr = [{name: 'Michael',children: ['Lara']},{name: 'Susy',      children: ['Mike', 'Ana', 'Mary']},{name: 'Bruno',children: ['Clark']}]

const map = data => data.map(parent => (
    parent.children.map(
        child => ({ name: child })), parent
))

console.log(JSON.stringify(map(currentArr), null, 4))
&#13;
&#13;
&#13;

注意:

与c ++ ,运算符不同,js ,中的运算符返回没有留下的rigth端

&#13;
&#13;
const data = (1,2,3,4,5,6)

console.log(data)
&#13;
&#13;
&#13;

答案 4 :(得分:-1)

var currentObj = [ 
  { name: 'Michael',   
    children: ['Lara'] 
  },
  { name: 'Susy',    
    children: ['Mike','Ana','Mary']
  },
  { name: 'Bruno', 
    children: ['Clark']
  }
]

for (var i=0, j=currentObj.length; i<j; i++) {
  currentObj[i].children = currentObj[i].children.map(function(item){return {name: item};})  
}
      
console.log(currentObj);