0

I have an array of objects like this:

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",
  }
]

I get:

[
  {
    "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)

I want:

{
  {
    "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 Answers 4

1

You can use the reduce() method instead of map().

Like so:

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

The difference is that when you use map, you return an array. Whereas with reduce you return whatever the result of your accumulator is, in this case it is an object that then gets patched into your parent object.

This can then be applied to your entire transform like so:

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;
}, {})
Sign up to request clarification or add additional context in comments.

2 Comments

I want an Object of objects, something like this...{ { "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": "", } } } ....Current solution returns array of objects
You can extend the principle of replacing map with reduce at the top level as well. I will update my answer
1

There you go, you were close

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)

Comments

1

Best (and obvious) solution: Refactor your code in a way that it returns what you want (others already provided approaches).

But, for the sake of more general solution (in case you only had the result you reported and not the original data), you can transform it according your specifications:

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);

2 Comments

Where I put yourResult goes your arrObj variable (I just edited my answer to match your code, so now you can simply paste it at the end of your own one and see the result). Anyway, as I said, the right solution is to modify your code, not to transform a wrong result. I did it just for fun and posted it because I think it could be interesting in cases were you aren't able to modify the original code. ;-)
...As a matter of fact I forgave to remove a JSON.stringify() call from my testing stuff (fixed). Sorry.
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.