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?

2
  • you can define the variable(lets say prefix) in environment.json file and use the value of prefix to split the array as your doing currently. Commented Sep 19, 2019 at 10:55
  • I couldn't understand the code. Can you be more clear with the output of the foreach? Commented Sep 19, 2019 at 10:55

4 Answers 4

2

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

console.log(/\d+/.exec('test1')) --> ["1"]
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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
i edited my example to resemble what i'm meaning, check the second array
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

Comments

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

1 Comment

with this I get core.js:1449 ERROR TypeError: Cannot read property '1' of null

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.