簡體   English   中英

如何從javascript中的嵌套對象數組中獲取對象

[英]How to get object from nested object array in javascript

我想知道如何使用 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"]
       }
     ]
}]

您可以使用地圖過濾器

使用過濾器,我們只取出匹配的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)

您可以使用mapfilter組合。 地圖將返回一個新數組,並在地圖回調函數中創建並返回一個帶有鍵idcountry_from的對象。 填充country_from的值時,使用filter僅提取國家/地區為“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)

您可以使用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);

使用 Map & Filter 並按如下方式更改函數:

 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM