1

Giving this in js:

let data={"vets":[{"id": 1,"lt": "6","ln": 2702,"ls": "2011-07-02T00:00:00"}]};

I want to use the split() function on the ls property.

data['vets'].ls is giving the value: "2011-07-02T00:00:00".

I save it to a variable like this:

let str = data['vets'].ls;

When i run str.split('T')[0] to get only 2011-07-02, i get an error of:

Uncaught TypeError: Cannot read property 'split' of null

Edit: The array i have is about 3000 items. I am using the map() functionn to go on all of them like this:

data['vets'].map(function(vet){
   let str = vet['ls'];
   let short = str.split('T');
   vet['ls'] = short[0];
})

What can be the reason for it? Thanks.

12
  • 2
    It should be data['vets'][0].ls because data['vets'] is an array. Commented Aug 2, 2019 at 13:02
  • 1
    data['vets'].ls is not returning "2011-07-02T00:00:00", it's returning undefined since vets is an array. Commented Aug 2, 2019 at 13:02
  • The array is for more than 3000 items. @CuongLeNgoc Commented Aug 2, 2019 at 13:03
  • 1
    How many elements are in the array is irrelevant to your question...unless you aren't fully explaining your situation. Maybe start with providing more context? Commented Aug 2, 2019 at 13:06
  • 1
    @GabMic okay I think I still don't understand your issue. At face value your code does what you seem to expect: change ls "YYYY-MM-DDTHH:MM:SS" to just "YYYY-MM-DD" i.imgur.com/lsvVOp2.png Commented Aug 2, 2019 at 13:14

1 Answer 1

2

Try data['vets'][0].ls.split('T')[0].

In other words, you need to first access the first child of the data[vets] array and perform a split on its ls field. What you are doing instead, is calling split on an ls property of the array itself, which is indeed undefined.

To get only the "date" part from each item:

let data = {"vets":[{"id": 1,"lt": "6","ln": 2702,"ls": "2011-07-02T00:00:00"}]};

console.log(data.vets.map(entry => entry.ls.split('T')[0]));

// shorter version which does the same thing
console.log(data.vets.map(({ ls }) => ls.split('T')[0]));

To create a new array of items with modified ls fields:

let data = {"vets":[{"id": 1,"lt": "6","ln": 2702,"ls": "2011-07-02T00:00:00"}]};

// splits every item into two variables: `ls` and `rest`. `ls` is a string, 
// whereas `rest` is an object containing all of the fields of the original
// item. Using these variables, modified objects are created and assembled
// into a new array in an immutable way (the original data is not altered)
console.log(data.vets.map(({ ls, ...rest }) => ({
  // replaces the `ls` field's value
  ls: ls.split('T')[0],
  // includes all the other properties untouched
  ...rest,
})));

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

1 Comment

The giving item above is just the first item in a larger (3000) items. i am using the map function to run on all of them. How can i deal with it in that case?

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.