-2

Due to the great support from SO users on my initial issue here I could transfer the suggested solution to my certain project environment and could -almost- make it happen.

I have a returned array object resultVolume with the following structure:

0: {x: "AUDUSD", y: 680, count: 10}
1: {x: "EURCAD", y: 690, count: 9}
2: {x: "USDCAD", y: 250, count: 8}
3: {x: "EURHUF", y: 600, count: 8}
4: {x: "CADCHF", y: 560, count: 7}
5: {x: "AUDNZD", y: 320, count: 7}
6: {x: "AUDCHF", y: 330, count: 7}
7: {x: "EURNOK", y: 590, count: 7}
8: {x: "EURJPY", y: 70, count: 7}
9: {x: "EURAUD", y: 430, count: 6}
10: {x: "EURSGD", y: 50, count: 5}
11: {x: "EURCHF", y: 50, count: 5}
12: {x: "GBPUSD", y: 370, count: 4}
13: {x: "AUDJPY", y: 80, count: 4}
14: {x: "CHFJPY", y: 240, count: 4}
15: {x: "USDJPY", y: 170, count: 4}
16: {x: "SGDJPY", y: 40, count: 4}
17: {x: "AUDCAD", y: 40, count: 4}

enter image description here

I now would like to do both

  • cut all objects after the 5th one (so after object number 4) and
  • remove the count property from all of the objects.

I tried the following:

delete resultCount.count;
const finalResultCount = resultCount.slice(0, 5);

which returns

0: {x: "AUDUSD", y: 680, count: 10}
1: {x: "EURCAD", y: 690, count: 9}
2: {x: "USDCAD", y: 250, count: 8}
3: {x: "EURHUF", y: 600, count: 8}
4: {x: "CADCHF", y: 560, count: 7}
5: {x: "AUDNZD", y: 320, count: 7}

with the count property not being removed.

Following these solutions I am not sure how to access each object since they all have different numbers in front. So probably I need a loop to achieve the desired outcome?

JSRB
  • 2,492
  • 1
  • 17
  • 48
  • Have a look at [Array.map()](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/map). Something like `resultCount.map(({x, y}) => ({x, y})).slice(0, 4)` should work. – soywod Dec 23 '19 at 10:35

4 Answers4

1

const arr = [{x: "AUDUSD", y: 680, count: 10},
{x: "EURCAD", y: 690, count: 9},
{x: "USDCAD", y: 250, count: 8},
{x: "EURHUF", y: 600, count: 8},
{x: "CADCHF", y: 560, count: 7},
{x: "AUDNZD", y: 320, count: 7}]

arr.forEach((item, index, array) => delete array[index].count);

console.log(arr);
AlexDevTime
  • 166
  • 4
1

var arr = [{
  x: "AUDUSD",
  y: 680,
  count: 10
}, {
  x: "EURCAD",
  y: 690,
  count: 9
}, {
  x: "USDCAD",
  y: 250,
  count: 8
}, {
  x: "EURHUF",
  y: 600,
  count: 8
}, {
  x: "CADCHF",
  y: 560,
  count: 7
}, {
  x: "AUDNZD",
  y: 320,
  count: 7
}, {
  x: "AUDCHF",
  y: 330,
  count: 7
}, {
  x: "EURNOK",
  y: 590,
  count: 7
}, {
  x: "EURJPY",
  y: 70,
  count: 7
}, {
  x: "EURAUD",
  y: 430,
  count: 6
}, {
  x: "EURSGD",
  y: 50,
  count: 5
}, {
  x: "EURCHF",
  y: 50,
  count: 5
}, {
  x: "GBPUSD",
  y: 370,
  count: 4
}, {
  x: "AUDJPY",
  y: 80,
  count: 4
}, {
  x: "CHFJPY",
  y: 240,
  count: 4
}, {
  x: "USDJPY",
  y: 170,
  count: 4
}, {
  x: "SGDJPY",
  y: 40,
  count: 4
}, {
  x: "AUDCAD",
  y: 40,
  count: 4
}];

var newArr = arr.slice(0, 5).map(e => {
  delete e.count;
  return e;
});

console.log(newArr);
Azad
  • 5,144
  • 4
  • 28
  • 56
1
let finalResultCount = resultCount.slice(0, 5);
finalResultCount.forEach(item => delete item.count);
treecon
  • 2,415
  • 2
  • 14
  • 28
0

var resultVolume = [
  {x: "AUDUSD", y: 680, count: 10},
  {x: "EURCAD", y: 690, count: 9},
  {x: "USDCAD", y: 250, count: 8},
  {x: "EURHUF", y: 600, count: 8},
  {x: "CADCHF", y: 560, count: 7},
  {x: "AUDNZD", y: 320, count: 7},
  {x: "AUDCHF", y: 330, count: 7},
  {x: "EURNOK", y: 590, count: 7},
  {x: "EURJPY", y: 70, count: 7},
  {x: "EURAUD", y: 430, count: 6},
  {x: "EURSGD", y: 50, count: 5},
  {x: "EURCHF", y: 50, count: 5},
  {x: "GBPUSD", y: 370, count: 4},
  {x: "AUDJPY", y: 80, count: 4},
  {x: "CHFJPY", y: 240, count: 4},
  {x: "USDJPY", y: 170, count: 4},
  {x: "SGDJPY", y: 40, count: 4},
  {x: "AUDCAD", y: 40, count: 4},
]
delete resultVolume.splice(5);
resultVolume.forEach(item => delete item.count);
console.log(resultVolume);
Arthur Grigoryan
  • 358
  • 3
  • 12