对象到数组(数组的数组) [英] Object to Array (an array of arrays)

查看:96
本文介绍了对象到数组(数组的数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用函数将对象文字转换为数组数组.

使用我拥有的两个示例对象,我要寻找的最终结果将是:

来自obj1

[ ["ugh","grr"] , ["foo", "bar"] , ["blah" , 138] ] 来自obj2

[ "shambala","walawala"] , ["foofighter","Barstool"] , ["blahblah",1382342453] ]

var obj1 = {
  ugh: "grr",
  foo: "Bar",
  blah: 138
}; 

var obj2 = {
  shambala: "walawala",
  foofighter: "Barstool",
  blahblah: 1382342453
};

var piece1 = Object.keys(obj1);

var piece2 = Object.values(obj1);

var result = [ ];

for (var i = 0; i < piece1.length; i++) {
 result.push([piece1[i] , piece2[i]])
 }

console.log(result)

从上面的内容,我已经能够实现:

来自obj1

[ ["ugh","grr"] , ["foo", "bar"] , ["blah" , 138] ]

但是我很困惑如何通过函数实现相同的输出.

这似乎很简单.

function objToArray(objectLiteral) {

var piece1 = Object.keys(objectLiteral);

var piece2 = Object.values(objectLiteral);

var result = [ ];

for (var i = 0; i < piece1.length; i++) {
 return result.push([piece1[i] , piece2[i]])
  }
 } 

console.log(objToArray(obj1))

这是我所能做的最好的事情,但是我不断得到1,但我不知道为什么. 其他尝试我最终都未定义.

解决方案

您的代码存在的问题是您使用return的时间早于所需的时间,请参见以下工作代码:

 var obj1 = {
  ugh: "grr",
  foo: "Bar",
  blah: 138
};

var obj2 = {
  shambala: "walawala",
  foofighter: "Barstool",
  blahblah: 1382342453
};

function objToArray(objectLiteral) {
  var piece1 = Object.keys(objectLiteral);
  var piece2 = Object.values(objectLiteral);
  var result = [];
  for (var i = 0; i < piece1.length; i++) {
    result.push([piece1[i], piece2[i]])
  }
  return result;
}

console.log(objToArray(obj1)); 

I am trying to convert an object literal into an array of arrays by using a function.

Using the two sample objects I have, the end result I'm looking for would be:

[ ["ugh","grr"] , ["foo", "bar"] , ["blah" , 138] ] from obj1

[ "shambala","walawala"] , ["foofighter","Barstool"] , ["blahblah",1382342453] ] from obj2

var obj1 = {
  ugh: "grr",
  foo: "Bar",
  blah: 138
}; 

var obj2 = {
  shambala: "walawala",
  foofighter: "Barstool",
  blahblah: 1382342453
};

var piece1 = Object.keys(obj1);

var piece2 = Object.values(obj1);

var result = [ ];

for (var i = 0; i < piece1.length; i++) {
 result.push([piece1[i] , piece2[i]])
 }

console.log(result)

From what I have above, I have been able to achieve:

[ ["ugh","grr"] , ["foo", "bar"] , ["blah" , 138] ] from obj1

But I am stumped about how to achieve the same output via a function.

This seems like a simple thing.

function objToArray(objectLiteral) {

var piece1 = Object.keys(objectLiteral);

var piece2 = Object.values(objectLiteral);

var result = [ ];

for (var i = 0; i < piece1.length; i++) {
 return result.push([piece1[i] , piece2[i]])
  }
 } 

console.log(objToArray(obj1))

This is what the best I can do but I keep on getting 1 and I don't know why. Other attempts I just end up with undefined.

解决方案

The problem with your code is you're using the return earlier than needed, see this working code:

var obj1 = {
  ugh: "grr",
  foo: "Bar",
  blah: 138
};

var obj2 = {
  shambala: "walawala",
  foofighter: "Barstool",
  blahblah: 1382342453
};

function objToArray(objectLiteral) {
  var piece1 = Object.keys(objectLiteral);
  var piece2 = Object.values(objectLiteral);
  var result = [];
  for (var i = 0; i < piece1.length; i++) {
    result.push([piece1[i], piece2[i]])
  }
  return result;
}

console.log(objToArray(obj1));

这篇关于对象到数组(数组的数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆