最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to find data in array vuejs - Stack Overflow

programmeradmin7浏览0评论

I want to find data by value, but the value is an array .. how?

 mylokasi [
    {
        id: 1 ,
            name : koko
    },
    {
        id: 2 ,
            name : doni
    },
    {
        id: 3 ,
            name : dika
    },
    {
        id: 4 ,
            name : ujang
    },
    ]

    mylokasi.find(p => p.id== [2,3,4,3]).name

I want to display all the data in the array [2,3,4,3]

result value so : doni,dika,ujang,dika

I want to find data by value, but the value is an array .. how?

 mylokasi [
    {
        id: 1 ,
            name : koko
    },
    {
        id: 2 ,
            name : doni
    },
    {
        id: 3 ,
            name : dika
    },
    {
        id: 4 ,
            name : ujang
    },
    ]

    mylokasi.find(p => p.id== [2,3,4,3]).name

I want to display all the data in the array [2,3,4,3]

result value so : doni,dika,ujang,dika

Share Improve this question edited Jan 26, 2022 at 5:21 Harry Wardana asked Jan 26, 2022 at 4:47 Harry WardanaHarry Wardana 2771 gold badge5 silver badges15 bronze badges 4
  • Loop the array, and grab the object property from the array index for example myLokasi[0].id would be valid, obviously many ways to achieve the desired result. a loop is just one. – MysticSeagull Commented Jan 26, 2022 at 4:50
  • can give an example? – Harry Wardana Commented Jan 26, 2022 at 4:52
  • [2,3,4,3].map(id => mylokasi.find(e => e.id == id).name) also I want to mention that qontol==penis in indonesia language idk why this guy use it as name example – buncis Commented Jan 26, 2022 at 5:04
  • let arrr = mylokasi.map((item) => item.name) gives you new array like ['koko', 'doni', 'dika', 'qotol'] – Ozal Zarbaliyev Commented Jan 26, 2022 at 5:05
Add a ment  | 

2 Answers 2

Reset to default 5

You can use the filter and map functions:

mylocasi
  .filter(item => [2, 3, 4, 3].includes(item.id))
  .map(item => item.name)

Using map() and find() you can achieve your goal !

Try this code it's help you !

  let mylokasi = [{ id: 1, name: 'koko' }, { id: 2, name: 'doni' }, { id: 3, name: 'dika' }, { id: 4, name: 'qotol' }];
    let arr = [2, 3, 4, 3];
    let res = arr.map((id) => (mylokasi.find(x => x.id == id).name));
    console.log(res, 'res');

发布评论

评论列表(0)

  1. 暂无评论