基于匹配键Javascript更新对象数组中的同一对象

时间:2018-04-23 11:52:39

标签: javascript arrays object

这个问题可能很愚蠢,但我仍然有如下数组。

[{"QId":"2030","answer":"~4591^Pain"},
 {"QId":"2030","answer":"~4592^Bad Breath"},
 {"QId":"2153","answer":"~5106^2ede"}, 
 {"QId":"2152","answer":"~5102^24fgf"}, 
 {"QId":"2154","answer":"~5109^No"},
 {"QId":"2152","answer":"~5109^New Answer"}]

我想合并匹配QId键的答案键。我想达到以下输出。

Output:
[{"QId":"2030","answer":"~4591^Pain~4592^Bad Breath"},
 {"QId":"2153","answer":"~5106^2ede"}, 
 {"QId":"2152","answer":"~5102^24fgf~5109^New Answer"}, 
 {"QId":"2154","answer":"~5109^No"}]

这是我到目前为止所尝试过的。



var AnswersOfQuestions = [{"QId":"2030","answer":"~4591^Pain"},
     {"QId":"2030","answer":"~4592^Bad Breath"},
     {"QId":"2153","answer":"~5106^2ede"}, 
     {"QId":"2152","answer":"~5102^24fgf"}, 
     {"QId":"2154","answer":"~5109^No"},
     {"QId":"2152","answer":"~5109^New Answer"}];

var output = [];

AnswersOfQuestions.forEach(function(val, index) {
  var filteredArray = AnswersOfQuestions.filter(function(obj) {
    return obj.QId === val.QId;
  });

  var tempString = "";

  var x = 0;
  for (; x < filteredArray.length; x++) {
    tempString = tempString + filteredArray[x].answer;
  }
  output.push({
    "QId": filteredArray[0].QId,
    "answer": tempString
  });

  tempString = null;
  x = null;

  console.log('output: ' + JSON.stringify(output));
});
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

Reduce将数组放入一个对象(r)中,每个对象的QId为关键字。如果QId中已找到某个对象的r,请将当前答案添加到该QId。如果不使用object spread(或Object.assign())来克隆它,并将其设置为r的值。使用Object.values()const data = [{"QId":"2030","answer":"~4591^Pain"},{"QId":"2030","answer":"~4592^Bad Breath"},{"QId":"2153","answer":"~5106^2ede"},{"QId":"2152","answer":"~5102^24fgf"},{"QId":"2154","answer":"~5109^No"},{"QId":"2152","answer":"~5109^New Answer"}]; const result = Object.values(data.reduce((r, o) => { if(o.QId in r) r[o.QId].answer += o.answer; else r[o.QId] = { ...o }; return r; }, Object.create(null))); console.log(result);转换回数组:

&#13;
&#13;
select 
VVO.VV_CODE, 
V.Vessel_name, 
VVO.Arrival_date, 
isnull(IGM.VIR_NO,'NULL') as VIR_NO, 
isnull(VVO.TERMINAL_CODE,'NULL') as TERMINAL_CODE,
(SELECT
COUNT(BL_NO)
FROM
BL_DATA
WHERE
VV_CODE = VVO.VV_CODE) as TOTAL_INDEX,
(select CAST(COUNT(VV_CODE) as int) from BL_Container  where Container_Size_Code = 19 and BL_Container.VV_CODE = VVO.VV_CODE ) as TWENTY_FT,
(select CAST(COUNT(VV_CODE) as int) from BL_Container  where Container_Size_Code = 20 and BL_Container.VV_CODE = VVO.VV_CODE ) as FOURTY_FT,
(select CAST(COUNT(VV_CODE) as int) from BL_Container  where (Container_Size_Code = 20 or Container_Size_Code = 19) and BL_Container.VV_CODE = VVO.VV_CODE ) as Total_No_of_Cntrs,
(Select((select cast(COUNT(VV_CODE) as float) from BL_Container where Container_Size_Code = 19 and BL_Container.VV_CODE = VVO.VV_CODE)/2 + (select COUNT(VV_CODE) from BL_Container where Container_Size_Code = 20 and BL_Container.VV_CODE = VVO.VV_CODE))) as Total_FFE
from Vessel_voyage VVO, Vessel V, IGM where V.Vessel_code = substring(VVO.VV_CODE,1,3) and VVO.VV_CODE = IGM.VV_CODE 
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以将array#reduceObject.values()一起使用。根据{{​​1}},concat QId对对象进行分组,以便在对象中重复answer。然后从这个对象中获取所有值。

&#13;
&#13;
QId
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这是一个创建Map的解决方案,由QId键入,然后在其中推送相应的答案,最后吐出结果:

const data = [{"QId":"2030","answer":"~4591^Pain"},{"QId":"2030","answer":"~4592^Bad Breath"},{"QId":"2153","answer":"~5106^2ede"},{"QId":"2152","answer":"~5102^24fgf"},{"QId":"2154","answer":"~5109^No"},{"QId":"2152","answer":"~5109^New Answer"}];

const map = new Map(data.map(o => [o.QId, []]));
data.forEach(o => map.get(o.QId).push(o.answer));
const result = Array.from(map, ([QId, v]) => ({ QId, answer: v.join("") }));

console.log(result);