我想知道如何使用JavaScript在输入对象中基于country_name
返回对象。
输入对象sampleobj
和变量source
作为参数传递给函数,函数随后应使用JavaScript基于source
返回输出obj,
我在下面尝试过
var source="TH";
var result = filterData(sampleobj, source);
function filterData(inputobj, src){
var filterByCountry = inputobj.filter((e)=>e.country_from.country.includes(src));
return filterByCountry;
}
var sampleobj = [
{
id: "trans",
country_from: [
{
country: "SG"
currency: ["SGD", "USD"]
},
{
country: "TH"
currency: ["THB", "USD"]
}
]
},
{
id: "fund",
country_from: [
{
country: "TH"
currency: ["THB", "USD"]
},
{
country: "UK"
currency: ["GBP", "USD"]
}
]
}
]
预期输出:
result =[{
id: "trans",
country_from: [
{
country: "TH"
currency: ["THB", "USD"]
}
]
},
{
id: "fund",
country_from: [
{
country: "TH"
currency: ["THB", "USD"]
}
]
}]
答案 0 :(得分:0)
使用过滤器,我们只取出匹配的country
并将其放回country_from's
值
let sampleobj = [{id: "trans",country_from: [{country: "SG",currency: ["SGD", "USD"]},{country: "TH",currency: ["THB", "USD"]}]},
{id: "fund",country_from: [{country: "TH",currency: ["THB", "USD"]},{country: "UK",currency: ["GBP", "USD"]}]}]
const source = 'TH'
const op = sampleobj.map(inp=>{
const country_from = inp.country_from.filter(({country})=> country === source)
return {
...inp,
country_from
}
})
console.log(op)
答案 1 :(得分:0)
您可以使用map
和filter
的组合。映射将返回一个新数组,并在映射回调函数内部创建并返回一个具有键id
和country_from
的对象。在填充country_from
的值时,请使用filter
仅提取country为“ TH”的那些对象
var sampleobj = [{
id: "trans",
country_from: [{
country: "SG",
currency: ["SGD", "USD"]
},
{
country: "TH",
currency: ["THB", "USD"]
}
]
},
{
id: "fund",
country_from: [{
country: "TH",
currency: ["THB", "USD"]
},
{
country: "UK",
currency: ["GBP", "USD"]
}
]
}
]
let result = sampleobj.map(function(item) {
return {
id: item.id,
country_from: item.country_from.filter(function(elem) {
return elem.country === 'TH'
})
}
})
console.log(result)
答案 2 :(得分:0)
您可以使用map
遍历数组,并使用filter
过滤country_from
var sampleobj = [{"id":"trans","country_from":[{"country":"SG","currency":["SGD","USD"]},{"country":"TH","currency":["THB","USD"]}]},{"id":"fund","country_from":[{"country":"TH","currency":["THB","USD"]},{"country":"UK","currency":["GBP","USD"]}]}];
var source = "TH";
let result = sampleobj.map(({id,country_from}) => ({id,country_from: country_from.filter(o => o.country === source)}));
console.log(result);
您可以添加另一个filter()
,仅返回带有country_from
的对象
var sampleobj = [{
"id": "trans",
"country_from": [{
"country": "SG",
"currency": ["SGD", "USD"]
}, {
"country": "TH",
"currency": ["THB", "USD"]
}]
},
{
"id": "fund", //Will not be returned since no TH on country_from
"country_from": [{
"country": "ID",
"currency": ["THB", "USD"]
}, {
"country": "UK",
"currency": ["GBP", "USD"]
}]
}
]
var source = "TH";
let result = sampleobj.map(({id,country_from}) => ({id,country_from: country_from.filter(o => o.country === source)}))
.filter(o => o.country_from.length);
console.log(result);
答案 3 :(得分:0)
使用“地图和过滤器”并按如下所示更改功能:
function filterData(inputobj, src) {
var filterByCountry = inputobj.map(({
id, country_from
}) => ({
id, country_from: country_from.filter(obj => obj.country.includes(src))
}));
return filterByCountry;
}
var sampleobj = [
{
id: "trans"
, country_from: [
{
country: "SG"
, currency: ["SGD", "USD"]
}
, {
country: "TH"
, currency: ["THB", "USD"]
}
]
}
, {
id: "fund"
, country_from: [
{
country: "TH"
, currency: ["THB", "USD"]
}
, {
country: "UK"
, currency: ["GBP", "USD"]
}
]
}
]
var source = "TH";
var result = filterData(sampleobj, source);
console.log(result)