2

I've got this JSON array (mdataarray)

[
{
"frumain" : "ESC",
"fruother" : "PAC or SEP or SPCB",
"connector" : "SE3 or SE7",
"sensor" : "ECHC, ECPC, ECCC, PCEC",
"motorsolenoid" : "ESCM",
"causeoferror" : "004, 101, 102, 103, 104, 106, 107, 108, 110, 111, 210, 211, 212, 213, 301, 302, 303, 304, 305, 306, 307, 312"
}
]

And I need to get just "causeoferror" object and slice it where the commas but I don't get it. I defined a new variable

public mdatacoe : string;

But when I do

this.mdatacoe = this.mdataarray.causeoferror;

In the console log I recieve undefined.

I'm pretty new to Typescript, I don't understand what I fail.

4
  • The array doesn't have that property, the object in it does. I'd strongly recommend reading up on basic JS. Commented Dec 27, 2019 at 19:55
  • Thanks, but which property? Commented Dec 27, 2019 at 20:05
  • What do you mean "which property"? The one you asked for, causeoferror. You should know it's an array, as you included that in the name. Commented Dec 27, 2019 at 20:07
  • @WilliamManzato You should be looking at this.mdataarray[0].causeoferror since the object is contained within an array. Commented Dec 28, 2019 at 0:33

3 Answers 3

1

Just take necessary object property of an array. Then you can split your string by comma sign ',':

const result = arr.map(({causeoferror}) => causeoferror.split(','));

An example:

let arr = [
    {
        "frumain": "ESC",
        "fruother": "PAC or SEP or SPCB",
        "connector": "SE3 or SE7",
        "sensor": "ECHC, ECPC, ECCC, PCEC",
        "motorsolenoid": "ESCM",
        "causeoferror": "004, 101, 102, 103, 104, 106, 107, 108, 110, 111, 210, 211, 212, 213, 301, 302, 303, 304, 305, 306, 307, 312"
    }
];

const result = arr.map(({causeoferror}) => causeoferror.split(','));
console.log(...result);

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

Comments

0

Considering your variable mdatacoe is of type string and not of type string[]:

I added a duplicate of the same json object into the array to mock multiple "causeoferror" properties. The map operator accesses the "causeoferror" property and the join operator converts the array to a string.

const foo = [
  {
    frumain: "ESC",
    fruother: "PAC or SEP or SPCB",
    connector: "SE3 or SE7",
    sensor: "ECHC, ECPC, ECCC, PCEC",
    motorsolenoid: "ESCM",
    causeoferror:
      "004, 101, 102, 103, 104, 106, 107, 108, 110, 111, 210, 211, 212, 213, 301, 302, 303, 304, 305, 306, 307, 312"
  },
  {
    frumain: "ESC",
    fruother: "PAC or SEP or SPCB",
    connector: "SE3 or SE7",
    sensor: "ECHC, ECPC, ECCC, PCEC",
    motorsolenoid: "ESCM",
    causeoferror:
      "004, 101, 102, 103, 104, 106, 107, 108, 110, 111, 210, 211, 212, 213, 301, 302, 303, 304, 305, 306, 307, 312"
  }
];

const bar = foo.map(x => x.causeoferror).join();

console.log(bar);

If you are indeed wanting an array of type string[], then @StepUp solution should be considered instead.

Comments

0

It's an array. change your code to this: this.mdatacoe = this.mdataarray[0].causeoferror;

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.