如何使用reduce和map新对象来解析JS中的重复值?

时间:2019-10-11 20:21:29

标签: javascript arrays ecmascript-6 reduce

我有来自后端的响应现在正在形成新对象,还尝试实现逻辑,如果有重复的clientname构建对象,如我预期的那样?我是否在减少方法中缺少未采用传播运算符的内容?什么是完成此任务的正确方法?

main.js

const data = [
  {
      clientname: "SDK_AETNA",
      preferencename: "apiKey",
      preferencevalue: "a4ddaf8d-b74e-4158-87fb"
  }, 
  {
      clientname: "SDK_CLOVER",
      preferencename: "apiKey",
      preferencevalue: "9e8323b3-93b3-4bc9-a02d"
  }, 
  {
      clientname: "SDK_RMN",
      preferencename: "apiKey",
      preferencevalue: "60470ae8-ac70-429c-806"
  }, {
      clientname: "SDK_RMN",
      preferencename: "plapiKey",
      preferencevalue: "7c4f6820-e799-410c-876"
  }
];

function mapData(data)
{
  var tempArray= [];
  var myOrderedArray = data.reduce(function (accumulator, currentValue) 
  {
    var newObj = {};
    newObj.clientName = accumulator.clientname;
    newObject.apiKey = accumulator.preferencevalue;
    tempArray.push(newObj);
    
    if (accumulator.clientname === currentValue.clientname) 
    {
      if (newObj.clientName === currentValue.clientname)
      {
        ...newObj,
          plapikey: currentValue.preferencevalue
      }
    }
    
    return tempArray
  }, []);


}

mapData(data);

const data = [{
        clientname: "SDK_AETNA",
        preferencename: "apiKey",
        preferencevalue: "a4ddaf8d-b74e-4158-87fb"
    }, {
        clientname: "SDK_CLOVER",
        preferencename: "apiKey",
        preferencevalue: "9e8323b3-93b3-4bc9-a02d"
    }, {
        clientname: "SDK_RMN",
        preferencename: "apiKey",
        preferencevalue: "60470ae8-ac70-429c-806"
    }, {
        clientname: "SDK_RMN",
        preferencename: "plapiKey",
        preferencevalue: "7c4f6820-e799-410c-876"
    }];

    function mapData(data){
      var tempArray= [];
     var myOrderedArray = data.reduce(function (accumulator, currentValue) {
     var newObj = {};
     newObj.clientName = accumulator.clientname;
     newObject.apiKey = accumulator.preferencevalue;
     tempArray.push(newObj);
      if (accumulator.clientname === currentValue.clientname) {
          if (newObj.clientName === currentValue.clientname){
              ...newObj,
                plapikey: currentValue.preferencevalue
          }

       }
      return tempArray
    }, [])


    }

    mapData(data);

预期结果

 [{
    clientname: "SDK_AETNA",
    apiKey: "a4ddaf8d-b74e-4158-87fb"
}, {
    clientname: "SDK_CLOVER",
    apiKey: "9e8323b3-93b3-4bc9-a02d"
}, {
    clientname: "SDK_RMN",
    apiKey: "60470ae8-ac70-429c-806",
    plapikey: "7c4f6820-e799-410c-876"
}];

3 个答案:

答案 0 :(得分:3)

reduce时,请使用从Map映射到所需对象的对象(或clientname),以便在找到重复对象时可以对其进行适当的更新。然后,您可以使用Object.values获得最终数组:

const data = [{
  clientname: "SDK_AETNA",
  preferencename: "apiKey",
  preferencevalue: "a4ddaf8d-b74e-4158-87fb"
}, {
  clientname: "SDK_CLOVER",
  preferencename: "apiKey",
  preferencevalue: "9e8323b3-93b3-4bc9-a02d"
}, {
  clientname: "SDK_RMN",
  preferencename: "apiKey",
  preferencevalue: "60470ae8-ac70-429c-806"
}, {
  clientname: "SDK_RMN",
  preferencename: "plapiKey",
  preferencevalue: "7c4f6820-e799-410c-876"
}];

const result = Object.values(data.reduce((acc, {clientname, preferencename, preferencevalue}) => {
  acc[clientname] = acc[clientname] || {clientname};
  acc[clientname][preferencename] = preferencevalue;
  return acc;
}, {}));

console.log(result);

答案 1 :(得分:0)

仅来自原始数据的reduce。 不需要额外的对象。

function mapData(data) {
  return data.reduce((acc, curr) => {
    let newAcc = [...acc];
    let targetIdx = newAcc.findIndex(v => v.clientname === curr.clientname);
    if (targetIdx === -1) {
      newAcc = [...newAcc, { clientname: curr.clientname }];
      targetIdx = newAcc.length - 1;
    }
    return [
      ...newAcc.slice(0, targetIdx),
      { ...newAcc[targetIdx], [curr.preferencename]: curr.preferencevalue },
      ...newAcc.slice(targetIdx + 1),
    ];
  }, []);
}

const data = [
  {
    clientname: 'SDK_AETNA',
    preferencename: 'apiKey',
    preferencevalue: 'a4ddaf8d-b74e-4158-87fb',
  },
  {
    clientname: 'SDK_CLOVER',
    preferencename: 'apiKey',
    preferencevalue: '9e8323b3-93b3-4bc9-a02d',
  },
  {
    clientname: 'SDK_RMN',
    preferencename: 'apiKey',
    preferencevalue: '60470ae8-ac70-429c-806',
  },
  {
    clientname: 'SDK_RMN',
    preferencename: 'plapiKey',
    preferencevalue: '7c4f6820-e799-410c-876',
  },
];

console.log(mapData(data));

答案 2 :(得分:0)

初始化reduce时,您以格式传递回叫函数

function callback(accumilator, value, index, array)

第二个参数是累加器的初始值。您的情况是[]

然后循环遍历方法并应用逻辑。在函数末尾,accumulator =回调的返回值。

例如:

var myArray = [2, 4, 6, 8];
var myObject = {};

function myCallback(accumalator, value, index, arr)
{
  accumalator[value] = value * 2;
  return accumalator;
}

myArray.reduce(myCallback, myObject);

console.log(myObject);

// Now wityh duplicate filtering

var filteredArr = [];
function FilterDups(accumalator, value, index, arr)
{
  if (!accumalator[value]) 
  {
    accumalator[value] = true;
    filteredArr.push(value);
  }
  
  return accumalator;
}

var duplicateData = [ 2, 2, 3, 4, 5, 6, 6, 6 ]
duplicateData.reduce(FilterDups, {});
console.log(filteredArr)

当它是一个空数组时,您将累加器视为数组中的值。其次,您的比较不正确