数组操作到新的数组对象

时间:2015-12-07 03:58:28

标签: javascript jquery arrays

我有一个混乱的阵列来自其他地方:

["person: John, Comment:, id_number:1, he is a guitar player","person: James, Comment:, id_number:38, James remark goes here"]

我怎样才能进入这个

[
{name:"John",id_number:1,Comment:"he is a guitar player"},
{name:"James",id_number:38,Comment:"James remark goes here"}
]

2 个答案:

答案 0 :(得分:0)

这样做:

=trim(right(substitute(a1, " ", rept(" ", len(a1))), len(a1)))

如果他们并不总是以正确的顺序,你可能会变得更加漂亮并尝试搜索出你想要的值。

这里有效:http://codepen.io/caleboleary/pen/bENGbx

祝你好运!

答案 1 :(得分:0)

以下是如何使用.map RegExp

转换数据的示例
["person: John, Comment:, id_number:1, he is a guitar player","person: James, Comment:, id_number:38, James remark goes here"].map(e => {
    var m = e.match(/^person: ([^,]+), Comment:, id_number:([^,]+), (.*)$/);
    return {"person": m[1], "id_number": +m[2], "Comment": m[3]};
});
// [
//    {"person": "John", "id_number": 1, "Comment": "he is a guitar player"},
//    {"person": "James", "id_number": 38, "Comment": "James remark goes here"}
// ]

我不能保证这会对API的所有输出起作用,因为它会产生错误的输出,实际上修复应该在源头

与API所有者联系