Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以逗号分隔字符串形式返回值为true的对象键名称_Javascript_Reactjs - Fatal编程技术网

Javascript 以逗号分隔字符串形式返回值为true的对象键名称

Javascript 以逗号分隔字符串形式返回值为true的对象键名称,javascript,reactjs,Javascript,Reactjs,我有一个具有不同属性的对象,我只需要为特定属性返回一个字符串=true { cash: true, cashless: true, credit: (4)[{…}, {…}, {…}, {…}], gift: true, stamp: true, stamp_number: 10, stamp_reward: "Free tea", terms_and_conditions: "Terms &

我有一个具有不同属性的对象,我只需要为特定属性返回一个字符串=true

{
    cash: true,
    cashless: true,
    credit: (4)[{…}, {…}, {…}, {…}],
    gift: true,
    stamp: true,
    stamp_number: 10,
    stamp_reward: "Free tea",
    terms_and_conditions: "Terms & Conditions",
    tier_type: "visits",
    upfront: false
}
我需要记录财产:礼物、邮票和预付款 在这种情况下,它应该返回一个string=Gift,Stamp

如果您需要为属性返回某些正确的大小写键名,并且值为
true
,您可以

  • 使用
  • 根据各自的值是否为
    true
    以及它们是否属于目标键列表(使用
  • 使用遍历结果列表以应用正确的套管
  • 将结果数组与
const src={
现金:没错,
无现金:是的,
信用证:[],
礼物:真的,
邮票:对,
邮票号码:10,,
邮票奖:“免费茶”,
条款和条件:“条款和条件”,
tier_类型:“访问”,
前期:假
},
结果=对象
.钥匙(src)
.filter(键=>['gift','stamp','upfront'])。包括(键)&&src[key]==true)
.map(key=>`${key[0].toUpperCase()}${key.slice(1)}`)
.join(“,”)

console.log(result)
我有这个按名称过滤的代码,但我需要它来只过滤真实的代码。const result=arr.filter((prop)=>prop=='gift'| | prop===''upfront'| | prop==''cash')。map(函数(字符串){return string.charAt(0).toupercase()+string.slice(1)}。join(','))