[英]Key-Value Pair Json transformation and conversion into a class in Javascript / Jquery
Below is the structure of my JSON in Javascript 以下是我在Javascript中的JSON的结构
[
[{"key": "firstname", "Value": "David"}, {"key": "lastname", "Value": "Smith"} ],
[{"key": "firstname", "Value": "Allen"}, {"key": "lastname", "Value": "Grover"} ],
[{"key": "firstname", "Value": "Randy"}, {"key": "lastname", "Value": "Paul"} ]
]
I want to transform this in Javascript / Jquery by removing "key" and "value" and make it 我想通过删除“键”和“值”并使其使其在Javascript / Jquery中进行转换
[
{"firstname" : "David", "lastname" : "Smith"},
{"firstname" : "Allen", "lastname" : "Grover"},
{"firstname" : "Randy", "lastname" : "Paul"}
]
And in addition am also looking to see if I can convert this json into javascript array of objects where I can access the properties like below would be great. 另外,我还希望查看是否可以将json转换为对象的javascript数组,以便可以访问如下所示的属性。
var people =[];
people[0].firstname = "David";
people[1].lastname = "Smith";
You can use .map()
and .reduce()
to achieve this functionality: 您可以使用.map()
和.reduce()
实现此功能:
var arr = [ [{"key": "firstname", "Value": "David"}, {"key": "lastname", "Value": "Smith"} ], [{"key": "firstname", "Value": "Allen"}, {"key": "lastname", "Value": "Grover"} ], [{"key": "firstname", "Value": "Randy"}, {"key": "lastname", "Value": "Paul"} ] ]; var final = arr.map(keys => { // Build the object, by attaching each key to the value return keys.reduce((obj, key) => { obj[key.key] = key.Value; return obj; }, {}); }); console.log(final);
Now, onto your question about handlebars/react/html framework. 现在,进入有关handlebars / react / html框架的问题。 Here is an example of how you can do this dirt simple in react: 这是一个示例,您可以通过简单的操作来实现此污垢:
var arr = [ [{"key": "firstname", "Value": "David"}, {"key": "lastname", "Value": "Smith"} ], [{"key": "firstname", "Value": "Allen"}, {"key": "lastname", "Value": "Grover"} ], [{"key": "firstname", "Value": "Randy"}, {"key": "lastname", "Value": "Paul"} ] ]; var final = arr.map(keys => { // Build the object, by attaching each key to the value return keys.reduce((obj, key) => { obj[key.key] = key.Value; return obj; }, {}); }); class App extends React.Component { constructor(props) { super(props); this.state = { data: props.data }; } render() { return React.createElement('div', null, this.state.data.map(name => React.createElement('div', null, `Name: ${name.firstname} ${name.lastname}` ) ) ); } } ReactDOM.render(React.createElement(App, {data: final}), document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div>
Note: This code is rather ugly, as I have to resort to using React.createElement
, something of which is not required when using JSX . 注意:这段代码很丑陋,因为我不得不诉诸于使用React.createElement
,使用JSX时不需要这些。
Just map
your initial data array: 只需map
您的初始数据数组即可:
const data = [ [{"key": "firstname", "Value": "David"}, {"key": "lastname", "Value": "Smith"} ], [{"key": "firstname", "Value": "Allen"}, {"key": "lastname", "Value": "Grover"} ], [{"key": "firstname", "Value": "Randy"}, {"key": "lastname", "Value": "Paul"} ] ]; const result = data.map(item => ({ [item[0]['key']]: item[0]['Value'], [item[1]['key']]: item[1]['Value'] })); console.log(result);
If the number of person objects per each data item could be > 2 then you need to update procedure, @FrankerZ answer is fine in that case. 如果每个数据项的人员对象数量可能大于2,则需要更新过程,在这种情况下,@ FrankerZ答案就可以了。 But if not, I would use explicit approach (without additional reducing) as more performant. 但是,如果没有,我会使用显式方法(无需额外减少)来提高性能。
It makes a pretty nice functional one-liner: 它是一个非常不错的功能一线:
let arr = [[{"key": "firstname", "Value": "David"}, {"key": "lastname", "Value": "Smith"} ],[{"key": "firstname", "Value": "Allen"}, {"key": "lastname", "Value": "Grover"} ],[{"key": "firstname", "Value": "Randy"}, {"key": "lastname", "Value": "Paul"} ]] let o = arr.map(item => item .reduce((a, {key, Value}) => ({[key]: Value, ...a} ), {})) console.log(o)
I'll do it as: 我将这样做:
const ary = [
[
{
key: "firstname",
Value: "David"
},
{
key: "lastname",
Value: "Smith"
}
],
[
{
key: "firstname",
Value: "Allen"
},
{
key: "lastname",
Value: "Grover"
}
],
[
{
key: "firstname",
Value: "Randy"
},
{
key: "lastname",
Value: "Paul"
}
]
];
let finalResult = ary.map(entry => {
const result = {};
entry.forEach(obj => {
result[obj.key] = obj.Value;
});
return result;
});
console.log(JSON.stringify(finalResult, null, 2));
// [
// {
// "firstname": "David",
// "lastname": "Smith"
// },
// {
// "firstname": "Allen",
// "lastname": "Grover"
// },
// {
// "firstname": "Randy",
// "lastname": "Paul"
// }
// ]
You can do it by map
您可以按map
来做
var arrList = [ [{"key": "firstname", "Value": "David"}, {"key": "lastname", "Value": "Smith"} ], [{"key": "firstname", "Value": "Allen"}, {"key": "lastname", "Value": "Grover"} ], [{"key": "firstname", "Value": "Randy"}, {"key": "lastname", "Value": "Paul"} ] ]; var readyResult = arrList.map(start => { resultSet = {}; start.forEach(obj => { resultSet[obj.key] = obj.Value; }); return resultSet; }); console.log(readyResult);
var data = [ [{ "key": "firstname", "Value": "David" }, { "key": "lastname", "Value": "Smith" }], [{ "key": "firstname", "Value": "Allen" }, { "key": "lastname", "Value": "Grover" }], [{ "key": "firstname", "Value": "Randy" }, { "key": "lastname", "Value": "Paul" }] ]; var result = data.map(v1 => { var vtemp = v1.map(v2 => ({ [Object.values(v2)[0]]: Object.values(v2)[1] })) return { ...vtemp[0], ...vtemp[1] } }) console.log(result); console.log(result[0].firstname);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.