如何从JavaScript中的嵌套对象数组获取对象

时间:2019-04-15 04:04:14

标签: javascript jquery arrays object

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

4 个答案:

答案 0 :(得分:0)

您可以使用mapfilter

使用过滤器,我们只取出匹配的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)

您可以使用mapfilter的组合。映射将返回一个新数组,并在映射回调函数内部创建并返回一个具有键idcountry_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)