2016-06-07 45 views
0

所以我一直在这个几个小时难住,我不能真正找出一个优雅的解决方案来解决这个问题。假设我有这样一个数组:Javascript - 如何将数组列表转换为另一个数组列表中的键值?

[ 
    { 
    question: "what is your name?", 
    answer: "Ben", 
    topic: "names" 
    }, 
{ 
    question: "what is your name?", 
    answer: "Ben", 
    topic: "names" 
    }, 
    { 
    question: "What is dog's name?", 
    answer: "Snugglets", 
    topic: "names" 
    }, 
    { 
    question: "What is your brother's age?", 
    answer: 55, 
    topic: "ages" 
    } 
] 

我怎样才能把它变成一个看起来像这样的数组?

[ 
    { 
    topic: "names", 
    content: [...array of objects based on "names"...] 
    }, 
{ 
    topic: "ages", 
    content: [...array of objects based on "ages"...] 
    } 
] 

我觉得这应该很容易,但由于某种原因,我的大脑似乎无法把握解决方案。我在这里错过了什么?

UPDATE:感谢您的所有回应!我并不期望得到这么多方法来完成这个任务。我接受了其中一个答案,因为它与ES6有关,我应该指定我希望学习ES6的方法,如果可能的话):

虽然,我也必须说需求也有改变地方,而不是预期排列了一下,是这样的:

[ { topic: "names", content: [...array of objects based on "names"...] }, { topic: "ages", content: [...array of objects based on "ages"...] } ]

的阵列需要这个样子更像:

[ { topic: "names", content: '<div> <h3>Question: What is your name?</h3> <p>Answer: Ben</p> </div> <div> <h3>Question: What is your dog's name?</h3> <p>Answer: Snugglets</p> </div>' }, { topic: "ages", content: content: '<div> <h3>Question: What is your age?</h3> <p>Answer: 50</p> </div> <div> <h3>Question: What is your brother's age?</h3> <p>Answer: 52</p> </div>' } ]

通常情况下,我不LIK e只是要求提供给我的代码,但我的Javascript foo在算法转换数据方面似乎有点弱点:(关于如何将所有这些数组元素合并为一个包含HTML的单个字符串,作为每个“主题”的“内容”键?

另外,这将更好地服务于Stackoverflow的另一个问题?

回答

1

使用ES6 Map对象时可以简单地完成如下操作。我还从content数组的项中删除了多余的names属性。

var data = [{question: "what is your name?",answer: "Ben",topic: "names"},{question: "what is your name?",answer: "Ben",topic: "names"},{question: "What is dog's name?",answer: "Snugglets",topic: "names"},{question: "What is your brother's age?",answer: 55,topic: "ages"}], 
 
mapData = data.reduce((p,c) => p.has(c.topic) ? p.set(c.topic,p.get(c.topic).concat({question:c.question, 
 
                         answer:c.answer})) 
 
               : p.set(c.topic,[{question:c.question, 
 
                    answer:c.answer}]),new Map()), 
 
    result = [...mapData].map(e => ({topic:e[0],content:e[1]})); 
 
console.log(result);

3

您可以将它与临时对象进行分组,并使用Array#forEach进行迭代。

forEach()方法每个数组元素一次执行设置功能。

var data = [{ question: "what is your name?", answer: "Ben", topic: "names" }, { question: "what is your name?", answer: "Ben", topic: "names" }, { question: "What is dog's name?", answer: "Snugglets", topic: "names" }, { question: "What is your brother's age?", answer: 55, topic: "ages" }], 
 
    group = []; 
 

 
data.forEach(function (a) { 
 
    if (!this[a.topic]) { 
 
     this[a.topic] = { topic: a.topic, content: [] }; 
 
     group.push(this[a.topic]); 
 
    } 
 
    this[a.topic].content.push(a); 
 
}, Object.create(null)); 
 

 
console.log(group);

+0

我认为他想推'({问题:a.question,回答:a.answer})'不仅'(a.answer)'。 –

+0

@FlorianLemaitre,对我来说有点不清楚。 –

0

试试这个:

var input = [ 
 
    { 
 
    question: "what is your name?", 
 
    answer: "Ben", 
 
    topic: "names" 
 
    }, 
 
{ 
 
    question: "what is your name?", 
 
    answer: "Ben", 
 
    topic: "names" 
 
    }, 
 
    { 
 
    question: "What is dog's name?", 
 
    answer: "Snugglets", 
 
    topic: "names" 
 
    }, 
 
    { 
 
    question: "What is your brother's age?", 
 
    answer: 55, 
 
    topic: "ages" 
 
    } 
 
]; 
 
var tmp = input.reduce(function(topics, obj) { 
 
    topics[obj.topic] = topics[obj.topic] || {topic: obj.topic, content: []}; 
 
    topics[obj.topic].content.push(obj); 
 
    return topics; 
 
}, {}); 
 
var output = Object.keys(tmp).map(function(key) { 
 
    return tmp[key]; 
 
}); 
 
console.log(output);

0

有一个简单的功能为您服务。

var arr=[ 
    { 
    question: "what is your name?", 
    answer: "Ben", 
    topic: "names" 
    }, 
    { 
    question: "what is your name?", 
    answer: "Ben", 
    topic: "names" 
    }, 
    { 
    question: "What is dog's name?", 
    answer: "Snugglets", 
    topic: "names" 
    }, 
    { 
    question: "What is your brother's age?", 
    answer: 55, 
    topic: "ages" 
    } 
] 
function turnToOtherArray(arr){ 
    var result=[]; 
    for(var i=0,mamorize={};i<arr.length;i++){ 
     if(mamorize[arr[i].topic]){ 
      mamorize[arr[i].topic].push(arr[i].question) 
     }else{ 
      mamorize[arr[i].topic]=[arr[i].question] 
     } 
    } 
    for(var key in mamorize){ 
     result[result.length] = { "topic" : key , "content" : mamorize[key]} 
    } 
    return result; 
} 
console.log(turnToOtherArray(arr)); 
相关问题