简体   繁体   中英

Push Arrays' indexes into one object

How to push array indexes into one object where the correspondence between {key:value} in my example would be {authors[i]: quotes[i]} ?

Please check my codepen:

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

Thanks.

You could iterate authors and take the name as key and assign the item of quotes as property of the object.

 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); 

The answer to your question would be:

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

But the really simple and elegant solution here would be to place all your values in a single array from the start:

 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... */
 ];

Than you can go over your quotes array with a simple for:

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

Alternatively, depending on your needs you could structure your data in an object, with this structure:

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

You can use the for...of loop and ES6 destructing or Array#reduce to build a new object.

 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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM