0

I have a json array which looks like this:

data: Array(8)
0: (3) ["test1", "4.96", "150"]
1: (3) ["test2", "156.16666666666666", "150"]
2: (3) ["test3", "279.3695652173913", "92"]
3: (3) ["test4", "1718", "16"]
4: (3) ["test5", "2.375", "16"]
5: (3) ["test6", "2230.6875", "16"]
6: (3) ["test7", "23.75", "32"]

I have a method to split the array:

data.forEach(test => {
    result.run.data.push([
        Number(test[0].split('test')[1]),
        Number(test[1])
    ]);

    result.count.data.push([
        Number(test[0].split('test')[1]),
        Number(test[2])
    ]);
});

As you can see, i split the array at "test". My problem is, the test-string could be different. I want the same mapping to be done when the json looks slightly different although has the same structure.

For example the json array looks like this:

 data: Array(8)
0: (3) ["asdf1", "4.96", "150"]
1: (3) ["fasd2", "156.16666666666666", "150"]
2: (3) ["qwer3", "279.3695652173913", "92"]
3: (3) ["llll4", "1718", "16"]
4: (3) ["rwwe5", "2.375", "16"]
5: (3) ["ttgd6", "2230.6875", "16"]
6: (3) ["34227", "23.75", "32"]

I would like it to split the same way as i do the first array.

How can i change my method to split the array at the start of each row instead of a string?

J.Doe
  • 586
  • 2
  • 8
  • 30

4 Answers4

2

Instead of split You could consider using regex to extract a number from string

console.log(/\d+/.exec('test1')) --> ["1"]
MoxxiManagarm
  • 8,735
  • 3
  • 14
  • 43
1

you can create a function for the same and pass key as param something like

spiltData(data,splitKey){
data.forEach(test => {
    result.run.data.push([
        Number(test[0].split(splitKey)[1]),
        Number(test[1])
    ]);

    result.count.data.push([
        Number(test[0].split(splitKey)[1]),
        Number(test[2])
    ]);
});
}

then use like

 this.spiltData(data,'test')
 this.spiltData(data,'asdf')

I am not sure about result.count & result.run here but you can pass them as param as well

Update

if key can be any dynamic string and not known to you and there will be only one number in string then you can use regex to get the number from the end of a string show your code should look like

spiltData(data){
    data.forEach(test => {
        var matches=test[0].match(/\d+$/);
        result.run.data.push([
            Number( matches ? matches[0] : 0),
            Number(test[1])
        ]);

        result.count.data.push([
            Number(matches ? matches[0] : 0),
            Number(test[2])
        ]);
    });
    }
jitender
  • 10,238
  • 1
  • 18
  • 44
  • is there a way to do this standarized? my problem is that the values like "asdf" and "test" always change. I can't change the params each time – J.Doe Sep 19 '19 at 11:04
  • i edited my example to resemble what i'm meaning, check the second array – J.Doe Sep 19 '19 at 11:05
1

Just replace Number(test[0].split('test')[1])

with

Number(test[0].split('').filter(x => !isNaN(parseInt(x, 10))))

Try like this:

this.data.forEach(test => {
  this.result.run.data.push([
    Number(test[0].split('').filter(x => !isNaN(parseInt(x, 10)))),
    Number(test[1])
  ]);

  ...
});

See Working Demo

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
1
data.forEach(test => {
    result.run.data.push([
        Number(/(\d+)$/.exec(test[0])[1]),
        Number(test[1])
    ]);

    result.count.data.push([
        Number(/(\d+)$/.exec(test[0])[1]),
        Number(test[2])
    ]);
});
Hsuan Lee
  • 2,300
  • 1
  • 10
  • 18