2017-02-13 13 views
1

私の例で{key:value}の間の対応が{authors[i]: quotes[i]}になる1つのオブジェクトに配列インデックスをプッシュするには?アレイのインデックスを1つのオブジェクトにプッシュ

私codepenを確認してください:

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

感謝。

+3

の質問に関連するコードを追加してください。 pleaasも見ています。[mcve] –

+0

私はcodepenを投稿していませんか? – tholo

+0

@tholoコードは*サードパーティのサイトではなく、質問そのものでなければなりません。 –

答えて

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

+0

ありがとう、ニナ、まさに私が必要としたもの。 :) – tholo

+0

@tholoしかし、同じ人を二度引用するとどうなりますか? –

+0

これは何も学習の練習に過ぎません。今、あなたがそれを言いました、あなたはそれをどう避けますか? – tholo

1

あなたの質問への答えは次のようになります。

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

しかし、ここで本当にシンプルでエレガントなソリューションは、最初から1つのアレイ内のすべての自分の価値観を置くことであろう:

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

より以下のためのシンプルであなたの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... */ 
} 
1

あなたはfor...ofループを使用することができますES6 destructingまたは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);

関連する問題