js把筛选对象字符串

const arr = {
  fields: {
    market: {
      data: [
        { name: 'isHk', value: true},
        { name: 'isOvs', value: false },
      ],
    },
  },
};

要求是:在market中的data里的value筛选出value是true的,只要data里的value有一个是true,就返回'market'这个字符串,如果data里都是false,则返回空


<script>
    const arr = {
        fields: {
            market: {
                data: [
                    { name: 'isHk', value: true },
                    { name: 'isOvs', value: false },
                ],
            },
        },
    };
    var value = arr.fields.market.data.filter(i => i.value === true).length ? 'market' : ''
    alert(value)
</script>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
     
<script>
    const arr = {
        fields: {
            market: {
                data: [
                    { name: 'isHk', value: true },
                    { name: 'isOvs', value: false },
                ],
            },
        },
    };

    function getMarket(data) {
        return data.some(d => d.value) ? 'market' : '';
    }
    alert(getMarket(arr.fields.market.data));
</script>
</body>
</html>

<script>
      const arr = {
        fields: {
          market: {
            data: [
              { name: 'isHk', value: true },
              { name: 'isOvs', value: false },
            ],
          },
        },
      }

      let key = Object.keys(arr.fields).find((key) => arr.fields[key].data.find((demo) => demo.value) && key) || ''
      console.log('value: ', key)
    </script>
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632