如何从这个 json 数组中获取包含 "eJx2" 的标题中的 user_name?
how to get user_name from the title that has "eJx2" in it from this json array?
我想从其中一个包含“eJx2”的文件中获取 user_name。
我有这段代码,但这段代码打印了所有“user_name”。我只想获取其中包含“eJx2”的那些 user_name。
var titles = arry.data.map(el => el.title);
for (var k in titles) {
var never = titles[k].includes('eJx2');
if (never) {
var names = arry.data.map(el => el.user_name);
console.log(names);
}
}
这是 json :
{
data: [
{
id: '46475273517',
user_name: 'testtwo',
title: 'Hello this is my test for the eJx2',
is_set: true
},
{
id: '46471542013',
user_name: 'testone',
title: 'Hello this is my test for the eJx3',
is_set: false
},
{
id: '46474254233',
user_name: 'testthree',
title: 'Hello this is my test for the eJx7',
is_set: false
}
],
pagination: {
cursor: 'eyJiIjp7IkN1cnNvciI6ImV5SnpJam80TXpBeExqSTBNemcwTVRnME56WTVOQ3dpWkNJNlptRnNjMlVzSW5RaU9uUnlkV1Y5In0sImEiOnsiQ3Vyc29yIjoiZXlKeklqbzFOREV1T1RnMk56STNNall5TkRReE5Dd2laQ0k2Wm1Gc2MyVXNJblFpT25SeWRXVjkifX0'
}
}
使用数组filter
array.data.filter(item => item.title.includes("eJx2"))
var array = {
data: [
{
id: '46475273517',
user_name: 'testtwo',
title: 'Hello this is my test for the eJx2',
is_set: true
},
{
id: '46471542013',
user_name: 'testone',
title: 'Hello this is my test for the eJx3',
is_set: false
},
{
id: '46474254233',
user_name: 'testthree',
title: 'Hello this is my test for the eJx7',
is_set: false
}
],
pagination: {
cursor: 'eyJiIjp7IkN1cnNvciI6ImV5SnpJam80TXpBeExqSTBNemcwTVRnME56WTVOQ3dpWkNJNlptRnNjMlVzSW5RaU9uUnlkV1Y5In0sImEiOnsiQ3Vyc29yIjoiZXlKeklqbzFOREV1T1RnMk56STNNall5TkRReE5Dd2laQ0k2Wm1Gc2MyVXNJblFpT25SeWRXVjkifX0'
}
}
var filteredData = array.data.filter(item => item.title.includes("eJx2"))
console.log(filteredData)
使用filter()
过滤标题有'eJx2'和map()
只得到user_name
:
const arry = {
data: [
{
id: '46475273517',
user_name: 'testtwo',
title: 'Hello this is my test for the eJx2',
is_set: true
},
{
id: '46471542013',
user_name: 'testone',
title: 'Hello this is my test for the eJx3',
is_set: false
},
{
id: '46474254233',
user_name: 'testthree',
title: 'Hello this is my test for the eJx7',
is_set: false
}
],
pagination: {
cursor: 'eyJiIjp7IkN1cnNvciI6ImV5SnpJam80TXpBeExqSTBNemcwTVRnME56WTVOQ3dpWkNJNlptRnNjMlVzSW5RaU9uUnlkV1Y5In0sImEiOnsiQ3Vyc29yIjoiZXlKeklqbzFOREV1T1RnMk56STNNall5TkRReE5Dd2laQ0k2Wm1Gc2MyVXNJblFpT25SeWRXVjkifX0'
}
}
const result = arry.data.filter(({title}) => title.includes("eJx2")).map(el => el.user_name)
console.log(result)
我想从其中一个包含“eJx2”的文件中获取 user_name。 我有这段代码,但这段代码打印了所有“user_name”。我只想获取其中包含“eJx2”的那些 user_name。
var titles = arry.data.map(el => el.title);
for (var k in titles) {
var never = titles[k].includes('eJx2');
if (never) {
var names = arry.data.map(el => el.user_name);
console.log(names);
}
}
这是 json :
{
data: [
{
id: '46475273517',
user_name: 'testtwo',
title: 'Hello this is my test for the eJx2',
is_set: true
},
{
id: '46471542013',
user_name: 'testone',
title: 'Hello this is my test for the eJx3',
is_set: false
},
{
id: '46474254233',
user_name: 'testthree',
title: 'Hello this is my test for the eJx7',
is_set: false
}
],
pagination: {
cursor: 'eyJiIjp7IkN1cnNvciI6ImV5SnpJam80TXpBeExqSTBNemcwTVRnME56WTVOQ3dpWkNJNlptRnNjMlVzSW5RaU9uUnlkV1Y5In0sImEiOnsiQ3Vyc29yIjoiZXlKeklqbzFOREV1T1RnMk56STNNall5TkRReE5Dd2laQ0k2Wm1Gc2MyVXNJblFpT25SeWRXVjkifX0'
}
}
使用数组filter
array.data.filter(item => item.title.includes("eJx2"))
var array = {
data: [
{
id: '46475273517',
user_name: 'testtwo',
title: 'Hello this is my test for the eJx2',
is_set: true
},
{
id: '46471542013',
user_name: 'testone',
title: 'Hello this is my test for the eJx3',
is_set: false
},
{
id: '46474254233',
user_name: 'testthree',
title: 'Hello this is my test for the eJx7',
is_set: false
}
],
pagination: {
cursor: 'eyJiIjp7IkN1cnNvciI6ImV5SnpJam80TXpBeExqSTBNemcwTVRnME56WTVOQ3dpWkNJNlptRnNjMlVzSW5RaU9uUnlkV1Y5In0sImEiOnsiQ3Vyc29yIjoiZXlKeklqbzFOREV1T1RnMk56STNNall5TkRReE5Dd2laQ0k2Wm1Gc2MyVXNJblFpT25SeWRXVjkifX0'
}
}
var filteredData = array.data.filter(item => item.title.includes("eJx2"))
console.log(filteredData)
使用filter()
过滤标题有'eJx2'和map()
只得到user_name
:
const arry = {
data: [
{
id: '46475273517',
user_name: 'testtwo',
title: 'Hello this is my test for the eJx2',
is_set: true
},
{
id: '46471542013',
user_name: 'testone',
title: 'Hello this is my test for the eJx3',
is_set: false
},
{
id: '46474254233',
user_name: 'testthree',
title: 'Hello this is my test for the eJx7',
is_set: false
}
],
pagination: {
cursor: 'eyJiIjp7IkN1cnNvciI6ImV5SnpJam80TXpBeExqSTBNemcwTVRnME56WTVOQ3dpWkNJNlptRnNjMlVzSW5RaU9uUnlkV1Y5In0sImEiOnsiQ3Vyc29yIjoiZXlKeklqbzFOREV1T1RnMk56STNNall5TkRReE5Dd2laQ0k2Wm1Gc2MyVXNJblFpT25SeWRXVjkifX0'
}
}
const result = arry.data.filter(({title}) => title.includes("eJx2")).map(el => el.user_name)
console.log(result)