user1743703
user1743703

Reputation: 131

Edit string in Javascript

Is there a way to consistently exctract city names from strings that look like this:

{"id":25,"title":"Buenos Aires"}
{"id":26,"title":"Chicago"}

I was thinking about starting from 3 rd character from the end and then stopping at second quotation mark but I didnt find the way to do it.

Upvotes: 0

Views: 96

Answers (1)

Eriks Klotins
Eriks Klotins

Reputation: 4180

The data is JSON formatted, you can decode it to js objects directly

var a = JSON.parse('{"id":25,"title":"Buenos Aires"}'), 
    b = JSON.parse('{"id":26,"title":"Chicago"}');

console.log(a.title); // prints Buenos Aires
console.log(b.title); // prints Chicago

Upvotes: 2

Related Questions