映射对象以将对象转换为数组

时间:2017-06-18 13:35:19

标签: javascript arrays object

我在这里有这个功能:

var obj = {
  name: 'Holly',
  age: 35,
  role: 'producer'
};


function convertObjectToList(obj) {
 return Object.keys(obj).map(k => [k, obj[k]]);
}
convertObjectToList(obj);

此函数将数组转换为obj。所以,如果我有上面的obj,我会得到这样的东西:

[['name', 'Holly'], ['age', 35], ['role', 'producer']]

现在我想关注一下:

return Object.keys(obj).map(k => [k, obj[k]]);

分解,Object.keys基本上返回一个给定对象自己的可枚举属性的数组,并在数组中映射迭代,所以用它来表示。我试图理解上面的箭头功能,并尝试将其分解为更简单易懂的代码。

function convertObjectToList(obj) {
 Object.keys(obj).map(function(key){
     obj = [key, obj[key]];
 });
  console.log(obj);
}

但是这个没用。相反,它只返回[“role”,undefined]。

那里有没有人可以让我理解非专业术语并打破代码所以我会清楚地理解它。

抱歉,我是初学者。小白。

6 个答案:

答案 0 :(得分:3)

箭头函数与函数类似,但语法不同。普通函数使用语法

function(x, y){
    return x + y;
}

箭头函数使用单行语法

(x, y) => x + y

或多行语法

(x, y) => {
    return x + y;
}

在您的示例中,

return Object.keys(obj).map(k => [k, obj[k]]);

转换为

return Object.keys(obj).map(function(k) {
    return [k, obj[k]];
});

因为该函数接受一个参数k(在箭头之前)并返回[k, obj[i]](在箭头之后)。

答案 1 :(得分:1)

应该是这样

var obj = {
	name: 'Holly',
	age: 35,
	role: 'producer'
};


function convertObjectToList(obj) {

	return Object.keys(obj).map(function (k) {
		return [k, obj[k]];
	})
}
convertObjectToList(obj);

答案 2 :(得分:1)

这是你的第二个功能:

function convertObjectToList(obj) {
  Object.keys(obj).map(function(key){
   obj = [key, obj[key]];
  });
  console.log(obj);
}

这里有一些不合适的地方:

  1. 您的第二个功能没有返回值。所以函数调用convertObjectToList将返回undefined。
  2. 您正在使用数组obj重新分配[key, obj[key]]的值,这不是您想要做的。这就是为什么你得到['role',undefined],因为obj的最后一个键是role,并且由于obj已被修改为数组而不是对象,obj['role']将返回undefined。
  3. 您需要在地图内部返回值,否则返回数组中的每个元素都将是未定义的。
  4. 所以正确的代码如下:

    var obj = {
      name: 'Holly',
      age: 35,
      role: 'producer'
    };
    
    function convertObjectToList(obj) {
     return Object.keys(obj).map(function(key){
       let currElement = [key, obj[key]];
       return currElement
     });
    }
    
    var res = convertObjectToList(obj);
    
    console.log(res);

答案 3 :(得分:1)

使用箭头功能时,如果箭头功能只包含一行,则可以避免使用return语句。因此,当使用常规功能时,您需要手动返回

var obj = {
  name: 'Holly',
  age: 35,
  role: 'producer'
};

function convertObjectToList(obj) {
  return Object.keys(obj).map(function(k) {
    return [k, obj[k]]
  });
}

var result = convertObjectToList(obj);
console.log(result);

答案 4 :(得分:1)

Object.keys 返回一个数组,其中的元素是与直接在obj中找到的可枚举属性相对应的字符串。属性的顺序与手动循环对象的属性所给出的顺序相同。

dput(df)
structure(list(node1 = structure(c(1L, 13L, 24L, 35L, 46L, 57L, 
68L, 79L, 90L, 2L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 25L, 26L, 27L, 28L, 
29L, 30L, 31L, 32L, 33L, 34L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 
43L, 44L, 45L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 
58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 69L, 70L, 71L, 
72L, 73L, 74L, 75L, 76L, 77L, 78L, 80L, 81L, 82L, 83L, 84L, 85L, 
86L, 87L, 88L, 89L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 
100L, 3L), .Label = c("link1", "link10", "link100", "link11", 
"link12", "link13", "link14", "link15", "link16", "link17", "link18", 
"link19", "link2", "link20", "link21", "link22", "link23", "link24", 
"link25", "link26", "link27", "link28", "link29", "link3", "link30", 
"link31", "link32", "link33", "link34", "link35", "link36", "link37", 
"link38", "link39", "link4", "link40", "link41", "link42", "link43", 
"link44", "link45", "link46", "link47", "link48", "link49", "link5", 
"link50", "link51", "link52", "link53", "link54", "link55", "link56", 
"link57", "link58", "link59", "link6", "link60", "link61", "link62", 
"link63", "link64", "link65", "link66", "link67", "link68", "link69", 
"link7", "link70", "link71", "link72", "link73", "link74", "link75", 
"link76", "link77", "link78", "link79", "link8", "link80", "link81", 
"link82", "link83", "link84", "link85", "link86", "link87", "link88", 
"link89", "link9", "link90", "link91", "link92", "link93", "link94", 
"link95", "link96", "link97", "link98", "link99"), class = "factor"), 
    node2 = structure(c(1L, 9L, 10L, 11L, 12L, 13L, 14L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA), .Label = c("link1", "link10", "link11", "link12", 
    "link13", "link14", "link15", "link16", "link4", "link5", 
    "link6", "link7", "link8", "link9"), class = "factor"), node3 = structure(c(1L, 
    2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA), .Label = c("link1", 
    "link2"), class = "factor"), node4 = c(NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA), node5 = structure(c(1L, 12L, 19L, 20L, 
    21L, 22L, 23L, 24L, 25L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
    10L, 11L, 13L, 14L, 15L, 16L, 17L, 18L, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), .Label = c("link1", 
    "link10", "link11", "link12", "link13", "link14", "link15", 
    "link16", "link17", "link18", "link19", "link2", "link20", 
    "link21", "link22", "link23", "link24", "link25", "link3", 
    "link4", "link5", "link6", "link7", "link8", "link9"), class = "factor"), 
    node6 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), node7 = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA), node8 = structure(c(1L, 
    12L, 23L, 34L, 39L, 40L, 41L, 42L, 43L, 2L, 3L, 4L, 5L, 6L, 
    7L, 8L, 9L, 10L, 11L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
    20L, 21L, 22L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 
    33L, 35L, 36L, 37L, 38L, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA), .Label = c("link1", "link10", "link11", 
    "link12", "link13", "link14", "link15", "link16", "link17", 
    "link18", "link19", "link2", "link20", "link21", "link22", 
    "link23", "link24", "link25", "link26", "link27", "link28", 
    "link29", "link3", "link30", "link31", "link32", "link33", 
    "link34", "link35", "link36", "link37", "link38", "link39", 
    "link4", "link40", "link41", "link42", "link43", "link5", 
    "link6", "link7", "link8", "link9"), class = "factor"), node9 = structure(c(1L, 
    2L, 3L, 4L, 5L, 6L, 7L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA), .Label = c("link1", 
    "link2", "link3", "link4", "link5", "link6", "link7"), class = "factor"), 
    node10 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("node1", 
"node2", "node3", "node4", "node5", "node6", "node7", "node8", 
"node9", "node10"), class = "data.frame", row.names = c(NA, -100L
))

假设你正在传递你的物体,那么地图是按照你的结果建造的,这就是你的结果。

    var arr = ["a", "b", "c"];
alert(Object.keys(arr)); // chiama alert con argomento "0,1,2"

// array like object
var obj = { 0 : "a", 1 : "b", 2 : "c"};
alert(Object.keys(obj)); // chiama alert con argomento "0,1,2"

在您的代码中尝试此更改

function objToStrMap(obj) {
    const strMap = new Map();
    for (const k of Object.keys(obj)) {
        strMap.set(k, obj[k]);
    }
    return strMap;
}

答案 5 :(得分:0)

您需要了解地图功能如何工作以查看您做错了什么。 map函数采用一个回调函数,我们将其称为 Mapper 。此函数接受数组中的每个项目,对其执行一些操作,并返回操作的结果。 map函数本身在数组上循环,每次都将一个项目传递给mapper,然后将结果添加到 NEW 数组中。

这是使用一个好的旧for循环的map函数的实现:

function customMap(array, mapper) {
  var result = [];
  for (int i = 0; i < array.length; i++) {
    var temp = mapper(array[i]);
    result.push(temp);
  }
  return result;
}

现在我们可以将此实现与您的函数进行比较,看看您做错了什么:

  1. 您的mapper函数没有返回任何内容。因此,在上面的实现中, temp 的值将是未定义的,并且将被推送到结果数组。
  2. 您尝试将映射器应用于整个 obj 对象,而不是键数组中的每个项目。通过这样做,您可以更改 obj 变量的值,从而在第一次迭代后丢失其内容。
  3. 您的 convertObjectToList 本身没有返回任何内容,因此最后无需显示任何内容。
  4. 要解决此问题,您可以使用以下内容:

    var obj = {
      name: 'Holly',
      age: 35,
      role: 'producer'
    };
    
    
    function convertObjectToList(obj) {
     return Object.keys(obj).map(function(key) {
       var temp = [key, obj[key]];
       console.log('current item: ', temp);
       return temp;
     });
    }
    
    var result = convertObjectToList(obj);
    console.log('final result: ', result);

    最后,没有必要为一个菜鸟道歉 - 我们都是这样开始的。