【问题标题】:Push Arrays' indexes into one object将数组的索引推送到一个对象中
【发布时间】:2017-07-01 20:07:41
【问题描述】:

如何将数组索引推送到一个对象中,在我的示例中{key:value} 之间的对应关系为{authors[i]: quotes[i]}

请检查我的代码笔:

http://codepen.io/anon/pen/Ndezeo

谢谢。

【问题讨论】:

  • 请在问题中添加相关代码。也请看这里minimal reproducible example
  • 我不是把codepen贴出来了吗??
  • @tholo 代码必须在问题本身中而不是在第三方网站上。

标签: javascript jquery html arrays object


【解决方案1】:

您可以迭代authors 并将名称作为键并将quotes 的项目分配为对象的属性。

var quotes = [], 
    authors = [],
    object = {};

quotes[0] = "I have a new philosophy. I'm only going to dread one day at a time.";
authors[0] = "Charles Schulz";
quotes[1] = "Reality is the leading cause of stress for those in touch with it.";
authors[1] = "Jack Wagner";
quotes[2] = "Few things are harder to put up with than the annoyance of a good example.";
authors[2] = "Mark Twain";
quotes[3] = "The pure and simple truth is rarely pure and never simple.";
authors[3] = "Oscar Wilde";
quotes[4] = "There's no business like show business, but there are several businesses like accounting.";
authors[4] = "David Letterman";
quotes[5] = "Man invented language to satisfy his deep need to complain.";
authors[5] = "Lily Tomlin";

authors.forEach(function (k, i) {
    object[k] = quotes[i];
});

console.log(object);

【讨论】:

  • 谢谢尼娜,正是我需要的。 :)
  • @tholo 但是如果你引用同一个人两次会发生什么?
  • 没什么,这只是为了学习练习。现在,您已经提到了它,您将如何避免这种情况?
  • 实际上是最后一个值。
【解决方案2】:

您的问题的答案是:

 var combined = [];
 for (var i = 0; i < quotes.length; i++) {
    combined[authors[i]] = quotes[i]
 }
 console.log(combined);

但这里真正简单而优雅的解决方案是从一开始就将所有值放在一个数组中:

 var quotes = [
   {
     author: "Charles Schulz",
     quote: "I have a new philosophy. I'm only going to dread one day at a time."
   },
   {
     author:  "Jack Wagner",
     quote: "Reality is the leading cause of stress for those in touch with it."
   }
   /* etc... */
 ];

您可以使用简单的 for 遍历您的 quotes 数组:

console.log(quotes);
for (var i = 0; i < quotes.length; i++) {
   /* access each object like this: 
   quotes[i].author;
   quotes[i].quote;
   */
}

或者,根据您的需要,您可以使用以下结构在对象中构建数据:

quotes = {
  "Charles Schulz":"I have a new philosophy. I'm only going to dread one day at a time.",
  "Jack Wagner":"Reality is the leading cause of stress for those in touch with it."
  /* etc... */
}

【讨论】:

    【解决方案3】:

    您可以使用for...of 循环和ES6 析构或Array#reduce 来构建一个新对象。

    let quotes = [];
    let authors = [];
    let object = {};
    
    quotes[0] = "I have a new philosophy. I'm only going to dread one day at a time.";
    authors[0] = "Charles Schulz";
    quotes[1] = "Reality is the leading cause of stress for those in touch with it.";
    authors[1] = "Jack Wagner";
    quotes[2] = "Few things are harder to put up with than the annoyance of a good example.";
    authors[2] = "Mark Twain";
    quotes[3] = "The pure and simple truth is rarely pure and never simple.";
    authors[3] = "Oscar Wilde";
    quotes[4] = "There's no business like show business, but there are several businesses like accounting.";
    authors[4] = "David Letterman";
    quotes[5] = "Man invented language to satisfy his deep need to complain.";
    authors[5] = "Lily Tomlin";
    
    // for...of loop taking advantage of the new array method entries & using destructuring
    for (const [index, element] of authors.entries()) {
      if (!object[element])
    object[element] = quotes[index];
    }
    
    console.log('Result of using for...of loop:', object);
    
    // array method reduce: Setting an object as the initial value 
    const usingReduce = authors.reduce((obj, author, index) => {
      if (!obj[author])
    obj[author] = quotes[index];
    
      return obj; // always return initial value
    }, {}); // here I set an obj as the initial value
    
    console.log('Result of using Array#reduce: ', usingReduce);
    
     // using map to return an object containing the authors
     // { author: author } same key/value pairs can be shortened to ->  { author }
    const usingMap = authors.map((author, index, authorsArray) => ({
      author,
      quote: quotes[index]
    }));
    
    console.log('Result of using Array#map method: ', usingMap);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多