将JS对象转换为数组值对

时间:2019-09-26 03:16:24

标签: javascript arrays object

我想知道如何将对象的键值对转换为键值对数组。我尝试使用[root@managernode ~]# cat kubernetes-dashboard.yaml --- apiVersion: v1 kind: ServiceAccount metadata: labels: k8s-app: kubernetes-dashboard kubernetes.io/cluster-service: "true" addonmanager.kubernetes.io/mode: Reconcile name: kubernetes-dashboard-admin namespace: kube-system [root@managernode ~]# kubectl get pods --namespace=kube-system NAME READY STATUS RESTARTS AGE calico-kube-controllers-564b6667d7-qglhj 1/1 Running 0 22h calico-node-4cpv9 1/1 Running 0 22h coredns-5644d7b6d9-c8vj4 1/1 Running 0 22h coredns-5644d7b6d9-l8qft 1/1 Running 0 22h etcd-managernode 1/1 Running 0 22h kube-apiserver-managernode 1/1 Running 0 22h kube-controller-manager-managernode 1/1 Running 0 22h kube-proxy-fhfk7 1/1 Running 0 22h kube-scheduler-managernode 1/1 Running 0 22h kubernetes-dashboard-7c54d59f66-gvslw 1/1 Running 0 22h [root@managernode ~]# kubectl proxy Starting to serve on 127.0.0.1:8001 ,但是例如,如果有,它会为我提供2D数组:

Object.entries(data)

现在变成:

var o = {"name":"john", "lastname":"doe"}

但是我真正需要的是:[编辑]-更改为对象数组

var a = [["name","john"],["lastname","doe"]]

我一直在玩这个游戏,但是没有运气。有人可以引导我做这个吗?非常感谢。谢谢!

5 个答案:

答案 0 :(得分:1)

您要求的输出格式-["name":"john", "lastname":"doe"]是不可能的,因为数组不支持这种格式。取而代之的是,我们可以使用对象数组,每个对象根据需要具有一个键值对-[{"name":"john"},{"lastname":"doe"}]

这是代码-

var o = {"name":"john", "lastname":"doe"}

var a=Object.keys(o).map(key=> {
  var obj={};
  obj[key]=o[key];
  return obj;
})

console.log(a);
// [{"name":"john"},{"lastname":"doe"}]

答案 1 :(得分:1)

["name":"john", "lastname":"doe"]无效,因为arrays不能有[key, value]对。

但是数组可以有对象,每个对象可以有[key, value]对。

Object.entries()返回[key, value]对对象。我们可以访问它们并将其插入数组。

我们可以使用Array.map()来做到这一点。

var o = {"name": "john","lastname": "doe"};

var result = Object.entries(o).map(([key, value]) => {
  return {[key]: value};
});

console.log(result);

答案 2 :(得分:0)

如果需要键值对,则它们必须在对象内部。因此,您需要这样的对象数组:[{“ name”:“ john”},{“ lastname”:“ doe”}]

这是使用reduce函数实现的一种高级方法:

 let newArr = Object.keys(obj).reduce((accumulator, keyInObj, index)) => {
    let keyValuePair = {};
    keyValuePair[keyInObj] = obj[keyInObj];
    accumulator[index] = keyValuePair;
    return accumulator;
 }, [])
  • 结果:

  • newArr = [{“ name”:“ john”},{“ lastname”:“ doe”}]

[{“ name”:“ john”},{“ lastname”:“ doe”}]

或者这样:

let obj = { name: "john", lastName: "doe"};
let newArr = [];
let index = 0;
for(let keyInObj in obj) {
     let keyValuePair = {};
     keyValuePair[keyInObj] = obj[keyInObj];
     index = index + 1;
     newArr[index] = keyValuePair;
}
  • 结果:
  • newArr = [{“ name”:“ john”},{“ lastname”:“ doe”}]

答案 3 :(得分:0)

数组不能有key:value,您可以创建一个对象数组来做到这一点。很简单的方法。

var o = {"name":"john", "lastname":"doe"};

var A = [];

for(let key in o){
   obj = {};           // create an object
   obj[key] = o[key]; // create key value pair
   A.push(obj)        // insert into Array
 }
 console.log(A)

答案 4 :(得分:0)

有一个补丁,如果您仍希望获得预期的输出,则只需将大括号替换为方括号

var string = '{"name":"john", "lastname":"doe"}';
string = string.replace(/\{/g, "\["); 
string = string.replace(/\}/g, "\]");

console.log(string);