Как получить первое значение из массива внутри json?
Есть json, где выводится такое поле
"image_url":"one.jpg|two.jpg|three.jpg|four.jpg",
Весь ответ
[
{
"KEYWORDS": "тест",
"LINK": "https://test.ru/",
"IMAGE_URL":"one.jpg|two.jpg|three.jpg|four.jpg"
}]
Как мне получить первое значение one.jpg в переменную value.image_url
fetch('https://site.com/json')
.then(response => response.json())
.then(data => {
console.log(data.IMAGE_URL); //тут нужно получить 1 значение one
})
.catch((err)=>console.log(err))
Спасибо.
Ответы (1 шт):
Автор решения: Алексей Шиманский
→ Ссылка
let data = [{
"KEYWORDS": "тест",
"LINK": "https://test.ru/",
"IMAGE_URL":"one.jpg|two.jpg|three.jpg|four.jpg"
}];
let whatINeed = data[0].IMAGE_URL.split('|')[0];
let nameWithoutExt = whatINeed.split('.')[0];
console.log(whatINeed, nameWithoutExt);
let data = [{
"KEYWORDS": "тест",
"LINK": "https://test.ru/",
"IMAGE_URL":"one.jpg|two.jpg|three.jpg|four.jpg"
}];
let imgUrl = data[0].IMAGE_URL;
let indexOfTheFirstDot =imgUrl.indexOf('.');
let nameWithoutExt = imgUrl.substr(0, indexOfTheFirstDot);
console.log(nameWithoutExt);