-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?

1
  • Have a look at Array.map(). Something like resultCount.map(({x, y}) => ({x, y})).slice(0, 4) should work. Commented Dec 23, 2019 at 10:35

4 Answers 4

1
let finalResultCount = resultCount.slice(0, 5);
finalResultCount.forEach(item => delete item.count);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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

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.