I am trying to pass an array values as keys of an object and in that object there are two values for each key, and then I want to pass it into a list item. how I can do that. this what i have tried so far:
'use strict';
const bookTitles = [
'harry_potter_chamber_secrets',
'small_island',
'spook_street',
'war_and_peace',
'don_quixote',
'a_tale_of_two_cities',
'the_lord_of_the_rings',
'harry_potter_and_the_secret_stone',
'and_then_there_were_none',
'the_hobbit',
];
console.log(bookTitles);
function booksInfoF(array) {
for (var i = 0; i < array.length; i++){
var booksInfoTrial = { array[i]: [{ author: 'somebody', language: 'english'}]}
}
return booksInfoTrial;
}
var outPut;
outPut.booksInfoF(bookTitles);
listNode = document.createElement('ul');
liNode = document.createElement('LI');
textNode = document.createTextNode(outPut);
liNode.appendChild(textNode);
listNode.appendChild(liNode);
Here my solution.
var obj = [ 'harry_potter_chamber_secrets', 'small_island', 'spook_street', 'war_and_peace', 'don_quixote', 'a_tale_of_two_cities', 'the_lord_of_the_rings', 'harry_potter_and_the_secret_stone', 'and_then_there_were_none', 'the_hobbit', ].reduce((ac, cv, i) => Object.assign(ac, {[cv] : [{author: 'somebody', language: 'english'}]}), {}); console.log(obj);
Hope it will help you.
Are you looking for this?
<!DOCTYPE html> <html> <body> <ul id="myList"> </ul> <p>Click the button to append an item to the end of the list.</p> <button onclick="myFunction()">Try it</button> <script> var books = [ 'harry_potter_chamber_secrets', 'small_island', 'spook_street', 'war_and_peace', 'don_quixote', 'a_tale_of_two_cities', 'the_lord_of_the_rings', 'harry_potter_and_the_secret_stone', 'and_then_there_were_none', 'the_hobbit', ].reduce((ac, cv, i) => Object.assign(ac, {[cv] : {author: 'somebody', language: 'english'}}), {}); function myFunction() { var UL = document.createElement("UL"); for(bookname in books){ var book_name = bookname.split('_').join(' ').toUpperCase(); var book = books[bookname]; var LI = document.createElement("LI"); var TX = document.createTextNode(book_name); LI.appendChild(TX); UL.appendChild(TX); var _UL = document.createElement("UL"); var I1 = document.createElement("LI"); var t1 = document.createTextNode('author: '+book.author); I1.appendChild(t1); var I2 = document.createElement("LI"); var t2 = document.createTextNode('language: '+book.language); I2.appendChild(t2); _UL.appendChild(I1); _UL.appendChild(I2); UL.appendChild(document.createElement("LI").appendChild(_UL)); } document.getElementById("myList").appendChild(UL); } </script> </body> </html>
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.