我的源代码中有以下数组:
var employees = [
{
"Question":"question 1",
"Answer":"answer 1"
},
{
"Question":"question 1",
"Answer":"another answer 1"
},
{
"Question":"question 2",
"Answer":"answer 2"
}
];
我需要创建一个函数,该函数可以对与同一个问题相关的所有答案进行分组,并将信息放在JSON对象中
{
"Question":"question 1",
"Answer" : '[{"answer 1","another answer 1"}]'
},
{
"Question":"question 2",
"Answer" : '[{"answer2"}]'
}
答案 0 :(得分:4)
这一行有很多问题
'[{"answer 1","another answer 1"}]'
首先,因为它在引号内,所以它是一个字符串,而不是一个对象。其次,数组中有一个无效对象。
如果我理解正确,你正在寻找
的内容employee[x].answers = ['answer1','answer2'];
答案 1 :(得分:1)
假设,就像每个人都说的那样,你并不真正意味着你所写的内容,但是我们都理解的是,我对这个案例的建议是使用下划线,它对这种情况有完美的要求,你的代码会是:
var result = _.chain(employees)
.groupBy('Question')
.map(function(value, key) {
return {
Question: key,
Answer: _.pluck(value, 'Answer')
}
})
.value();
答案 2 :(得分:1)
你可以试试这个。调用此函数并将employees
数组传递给它,它将返回修改后的数组
function customJSON(employees){
var newEmp = [],tmp = 0;
for(var i=0; i<employees.length; i++){
if(employees[i]){
if(newEmp.indexOf(employees[i].Question)>-1){
employees[newEmp.indexOf(employees[i].Question)].Answer.push(employees[i].Answer);
employees.splice(i,1);
--i;
}else{
newEmp.push(employees[i].Question);
tmp = employees[i].Answer;
employees[i].Answer = [tmp];
}
}
}
return employees;
}
答案 3 :(得分:1)
var employees = [
{
"Question":"question 1",
"Answer":"answer 1"
},
{
"Question":"question 1",
"Answer":"another answer 1"
},
{
"Question":"question 2",
"Answer":"answer 2"
}];
var employeesNewDataFormat = [];
for(var i=0; i<employees.length; i++){
var hasEntry = false;
var index = -1;
for(var j=0; j<employeesNewDataFormat.length; j++)
{
if(employeesNewDataFormat[j].Question === employees[i].Question)
{
hasEntry = true;
index = j;
break;
}
}
if(hasEntry)
{
employeesNewDataFormat[index].Answer.push(employees[i].Answer);
}
else
{
employeesNewDataFormat.push({"Question":employees[i].Question, "Answer":[employees[i].Answer]});
}
}
console.log(JSON.stringify(employeesNewDataFormat));
答案 4 :(得分:1)
如评论中所述,您将答案写成字符串而不是字符串数组。
您可能想要实现的目标是:
var employees = [
{
"question":"question 1",
"answers": ["answer 1"]
},
{
"question":"question 2",
"answers": ["answer 2", "some other answer", "... etc"]
}
];
// add answers to question 1
employees[0].answers.push('a new anwser 1');
// add anwers to question 2
employees[1].answers.push('a new answer 2');
// ... and so on
如果你想要一个干净的方式来添加答案并直接将你的答案与你的问题相关,我会怎么看?
现在这个功能将是:
var employees = [
{
"Question":"question 1",
"Answer":"answer 1"
},
{
"Question":"question 1",
"Answer":"another answer 1"
},
{
"Question":"question 2",
"Answer":"answer 2"
}
];
function groupAnswersToQuestions(employees){
var questions = [], result = [];
// set all questions with same string to the same key
for(var k in employees){
// set the question to an array if not set yet
var q = employees[k].Question;
if( ! questions[q]) questions[q] = [];
// add the answer to the array
questions[q].push(employees[k].Answer);
}
// re render a new array with the wanted structure
for(var k2 in questions){
result.push({ question: k2, answers: questions[k2] });
}
return result;
}
alert( JSON.stringify( groupAnswersToQuestions(employees), null, 2) );
&#13;
答案 5 :(得分:1)
您可以使用地图来跟踪问题并添加附加到其上的阵列的答案:
var employees = [{
"Question":"question 1",
"Answer":"answer 1"
},
{
"Question":"question 1",
"Answer":"another answer 1"
},
{
"Question":"question 2",
"Answer":"answer 2"
}];
var questions = {};
for (var i = 0; i < employees.length; i++) {
if (questions[employees[i].Question] === undefined) {
questions[employees[i].Question] = [];
}
questions[employees[i].Question].push(employees[i].Answer)
}
alert(JSON.stringify(questions, null, 2))
答案 6 :(得分:1)
这有点粗糙和冗长,但它确实符合我的要求:
var questions = {};
for (var i = employees.length - 1; i >= 0; i--) {
var Question = '';
for (var property in employees[i]) {
// console.log(property);
var val = employees[i][property];
if (property == "Question") {
Question = val;
if (typeof questions[Question] == 'undefined') {
questions[Question] = [];
};
}
if(property == 'Answer') {
questions[Question].push(val);
}
};
};
var new_employees = [];
for (var Question in questions) {
var obj = {
"Question": Question,
"Answer": questions[Question]
}
new_employees.push(obj);
};
console.log(new_employees);