映射数组元素并返回对象

时间:2019-02-14 10:57:19

标签: javascript

我有一个这样的对象数组:

var pools = [{
    dce: 3,
    lts: 2,
    name: "nift nation",
  },
  {
    dce: 1049.99,
    lts: 104.999,
    name: "NSG I.NS. Mark Select",
  },
  {
    dce: 162,
    lts: 36.157,
    name: "Shift-Team Mark Select",
  }
]

我得到:

[
  {
    "nift_nation": [
      {
        "nift_nationDollars": ""
      },
      {
        "nift_nationUnits": ""
      },
      {
        "nift_nationPercentage": ""
      }
    ]
  },
  {
    "NSG_I$NS$_Mark_Select": [
      {
        "NSG_I$NS$_Mark_SelectDollars": ""
      },
      {
        "NSG_I$NS$_Mark_SelectUnits": ""
      },
      {
        "NSG_I$NS$_Mark_SelectPercentage": ""
      }
    ]
  },
  {
    "Shift__Team_Mark_Select": [
      {
        "Shift__Team_Mark_SelectDollars": ""
      },
      {
        "Shift__Team_Mark_SelectUnits": ""
      },
      {
        "Shift__Team_Mark_SelectPercentage": ""
      }
    ]
  }
]

var pools = [{
    dce: 3,
    lts: 2,
    name: "nift nation",
  },
  {
    dce: 1049.99,
    lts: 104.999,
    name: "NSG I.NS. Mark Select",
  },
  {
    dce: 162,
    lts: 36.157,
    name: "Shift-Team Mark Select",
  }
]

var getFieldSuffix = function(rowFieldCount) {
  switch (rowFieldCount) {
    case 0:
      return 'Dollars';
    case 1:
      return 'Units';
    case 2:
      return 'Percentage';
    default:
      return
  }
};

var replacementMap = {
  single_space: '_',
  dot: '$',
  hyphen: '__',
};

var replacer = function(str) {
  return str.replace(/[ .-]/g, l => {
    if (l == ".") return replacementMap["dot"];
    if (l == " ") return replacementMap["single_space"];
    return replacementMap["hyphen"];
  });
};

var arrObj = pools.map(function(pool) {
  return Object.assign({
    [replacer(pool.name)]: ['Dollars', 'Units', 'Percentage'].map(function(suffix, index) {
      return {
        [replacer(pool.name) + getFieldSuffix(index % 3)]: ''
      }
    })
  })
})

console.log(arrObj)

我想要:

{
  {
    "nift_nation": {
      "nift_nationDollars": "",
      "nift_nationUnits": "",
      "nift_nationPercentage": "",
    }
  },
  {
    "NSG_I$NS$_Mark_Select": {
      "NSG_I$NS$_Mark_SelectDollars": "",
      "NSG_I$NS$_Mark_SelectUnits": "",
      "NSG_I$NS$_Mark_SelectPercentage": "",
    }
  },
  {
    "Shift__Team_Mark_Select": {
      "Shift__Team_Mark_SelectDollars": "",
      "Shift__Team_Mark_SelectUnits": "",
      "Shift__Team_Mark_SelectPercentage": "",
    }
  } 
}

4 个答案:

答案 0 :(得分:1)

您可以使用reduce()方法代替map()。

像这样:

return Object.assign({
    [replacer(pool.name)]: ['Dollars', 'Units', 'Percentage'].reduce(function(acc, suffix, index) { 
        acc[replacer(pool.name) + getFieldSuffix(index % 3)] = ''; 
        return acc;
     }, {})
  })

区别在于,当您使用map时,您将返回一个数组。使用reduce可以返回累加器的结果,在这种情况下,它是一个对象,然后将其修补到父对象中。

然后可以将其应用于整个转换,如下所示:

var arrObj = pools.reduce(function(acc, pool) {
  acc[replacer(pool.name)] = ['Dollars', 'Units', 'Percentage'].reduce(function(acc, suffix, index) { 
        acc[replacer(pool.name) + getFieldSuffix(index % 3)] = ''; 
        return acc;
     }, {});
   return acc;
}, {})

答案 1 :(得分:1)

你去了,你很近

var pools = [{
    dce: 3,
    lts: 2,
    name: "nift nation",
  },
  {
    dce: 1049.99,
    lts: 104.999,
    name: "NSG I.NS. Mark Select",
  },
  {
    dce: 162,
    lts: 36.157,
    name: "Shift-Team Mark Select",
  }
]

var getFieldSuffix = function(rowFieldCount) {
  switch (rowFieldCount) {
    case 0:
      return 'Dollars';
    case 1:
      return 'Units';
    case 2:
      return 'Percentage';
    default:
      return
  }
};

var replacementMap = {
  single_space: '_',
  dot: '$',
  hyphen: '__',
};

var replacer = function(str) {
  return str.replace(/[ .-]/g, l => {
    if (l == ".") return replacementMap["dot"];
    if (l == " ") return replacementMap["single_space"];
    return replacementMap["hyphen"];
  });
};

var arrObj = Object.assign(...pools.map((pool) => {
  return {
    [replacer(pool.name)]: Object.assign(...['Dollars', 'Units', 'Percentage'].map(function(suffix, index) {
      return {
        [replacer(pool.name) + getFieldSuffix(index % 3)]: ''
      }
    }))
  }
}))

console.log(arrObj)

答案 2 :(得分:1)

最佳(也是显而易见的)解决方案:重构代码,使其返回所需的方式(其他已经提供的方法)。

但是,为了提供更通用的解决方案(如果您仅获得报告的结果而不是原始数据),可以根据您的规范进行转换:

const formattedResult = Object.assign.apply(null, arrObj.map(function(o){
    let k=Object.keys(o)[0];
    return {[k]:Object.assign.apply(null, o[k])};
}));
console.log(formattedResult);

答案 3 :(得分:0)

var pools = [{
    dce: 3,
    lts: 2,
    name: "nift nation",
  },
  {
    dce: 1049.99,
    lts: 104.999,
    name: "NSG I.NS. Mark Select",
  },
  {
    dce: 162,
    lts: 36.157,
    name: "Shift-Team Mark Select",
  }
]

var getFieldSuffix = function(rowFieldCount) {
  switch (rowFieldCount) {
    case 0:
      return 'Dollars';
    case 1:
      return 'Units';
    case 2:
      return 'Percentage';
    default:
      return
  }
};

var replacementMap = {
  single_space: '_',
  dot: '$',
  hyphen: '__',
};

var replacer = function(str) {
  return str.replace(/[ .-]/g, l => {
    if (l == ".") return replacementMap["dot"];
    if (l == " ") return replacementMap["single_space"];
    return replacementMap["hyphen"];
  });
};

var arrObj = pools.map(function(pool) {
  const obj = {};
  ['Dollars', 'Units', 'Percentage'].forEach(function(suffix, index) {
obj[replacer(pool.name) + getFieldSuffix(index % 3)] = ''
  })
  return Object.assign({
[replacer(pool.name)]: obj
  })
})

console.log(arrObj)