0

I have the following array, which returns the values ​​as follows:

0: data: (2) [10000, "Vinil s/ pó"] name: "Janeiro"

I'm trying to split this way:

var series1 = series.split(",");

var series2 = series1[1] + "," + series1[2];

But it gives me the following error:

Uncaught TypeError: series.split is not a function

Code to generate the array

var series = [],
    len = data.length,
    i = 0;
    
 for(i;i<len;i++){
    series.push({
        name: 'Janeiro',
        data:[data[i][7], data[i][3]]
    });
}

Link to draw the graph Link

11
  • 1
    If it's actually an array, there's no need to work at the string level. What you'e quoted as what you have isn't at all clear. Is it one big string? Is it an object with properties where one of the properties is an array? Something else? Commented Sep 17, 2020 at 10:42
  • 1
    @T.J.Crowder Yes, I agree, that's normally what I do as well (suggesting the language-specific SE site). But, since in this case it was just a single line, I believe that was just an OP's inattention, they probably understand English. Commented Sep 17, 2020 at 10:45
  • @T.J. Crowder I added the code that generates the array to try to explain my problem better. This problem arose when trying to solve the problem I posed in this : question Commented Sep 17, 2020 at 10:46
  • 1
    Bruno, please show us what result you want. Commented Sep 17, 2020 at 10:47
  • @T.J. Crowder Yes, it was inattention of my pie. I apologize Commented Sep 17, 2020 at 10:48

2 Answers 2

1

You don't need to split anything, your data is already in separate entries in the array, at series[index].data[0] (the number) and series[index].data[1] (the string). So you can access those in a loop, for instance:

// (`i` is already declared in the OP's code)
for (i = 0; i < series.length; ++i) {
    var num = series[i].data[0];
    var str = series[i].data[1];
    console.log(num, str);
}

Live Example:

var data = [
    [,,,"Vinil s/ pó",,,,10000],
    [,,,"Another value",,,,20000],
];
var series = [],
    len = data.length,
    i = 0;
    
 for(i;i<len;i++){
    series.push({
        name: 'Janeiro',
        data:[data[i][7], data[i][3]]
    });
}

// Using each entry:
for (i = 0; i < series.length; ++i) {
    var num = series[i].data[0];
    var str = series[i].data[1];
    console.log(num, str);
}

Or with ES2015+ language features (for-of, destructuring, and const):

// Using each entry
for (const {data: [num, str]} of series) {
    console.log(num, str);
}

Live Example:

var data = [
    [,,,"Vinil s/ pó",,,,10000],
    [,,,"Another value",,,,20000],
];
var series = [],
    len = data.length,
    i = 0;
    
 for(i;i<len;i++){
    series.push({
        name: 'Janeiro',
        data:[data[i][7], data[i][3]]
    });
}

// Using each entry
for (const {data: [num, str]} of series) {
    console.log(num, str);
}

Sign up to request clarification or add additional context in comments.

Comments

0

Series seems to be an array of objects, since split is a String method you cant use it on arrays nor objects.

Inside each object you have the key data which points to an array so you don't have to split it in the way you seem to be trying.

Just access series[index].data[innerArrIndex]

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.