Push Arrays'索引到一个对象

时间:2017-02-13 18:19:39

标签: javascript jquery html arrays object

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

请检查我的codepen:

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

感谢。

3 个答案:

答案 0 :(得分:3)

您可以迭代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);

答案 1 :(得分:1)

你的问题的答案是:

 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... */
}

答案 2 :(得分:1)

您可以使用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);