简体   繁体   English

如何在 forEach 循环中格式化重命名 json 键,该循环也在检查 json 键对的值

[英]How to format rename json key in a forEach loop that is also checking for the value of the json key pairs

I initially had the following json weightLoss and Strength are checkbox values either on or false我最初有以下 json weightLoss 和 Strength 是复选框值 on 或 false

{ 
  name: 'Anders',
  package: 'Silver',
  email: 'email@email.com',
  subject: 'fdsafa',
  weightLoss: 'on', // or false
  strength: false,
  message: 'fdsasfdsdafsdafasdfasd'
}

Which I then run the following for each loop to change the 'on' values (or != false ) to the name of the key然后我为每个循环运行以下命令以将“on”值(或!= false )更改为键的名称

Object.entries(payLoadReason).forEach(([key, value]) => {
        if (value != false) {
        payLoadReason[key] = key;
        }
    });

I then get this formatted json, if strenth were set to 'on' it's value would also change to it's key strength然后我得到这个格式化的 json,如果强度设置为“on”,它的值也会改变为它的关键strength

{ 
  name: 'name',
  package: 'package',
  email: 'email',
  subject: 'subject',
  weightLoss: 'weightLoss', // or false
  strength: false,
  message: 'message'
}

What I want to do next is to rename the key's of any value that was originally 'on' and change the key to reason so I end up with formatted Json like below so I can iterate through it in a send grid handlebars template我接下来要做的是重命名最初为“on”的任何值的键,并将键更改为reason所以我最终得到如下格式的 Json,以便我可以在发送网格把手模板中遍历它

{
"data":{
      "reasonArray":[
         {
            "reason":"weightLoss"
         },
         {
            "reason":"strength"
         }
      ]
   }
}

handle bars template把手模板

 <ol>
   {{#each data.reasonArray}}
    <li>{{this.reason}} </li>
  {{/each}}
 </ol>

The orignal Json is coming from the formState, so it is the whole forms state at submit.原始 Json 来自 formState,因此它是提交时的整个表单状态。

Update更新

Attempt from Diago's answer.尝试从迪亚戈的回答。 Results in just an array with the values.结果只是一个包含值的数组。 I need a key of reason with a value of the original key inside a reasonArray[] and a data {} Here is what I got from the answer我需要一个带有reasonArray[]data {}原始键值的原因键,这是我从答案中得到的

[
  'Anders',
  'Silver',
  'email@email.com', 
  'fdsafa',
  'on',
  false,
  'fdsasfdsdafsdafasdfasd'
]

Not sure if this is exactly what you want, but I feel you can use the reduce method to achieve your goal:不确定这是否正是您想要的,但我觉得您可以使用reduce方法来实现您的目标:

const normalizedData = Object.values(payLoadReason).reduce((acc, cur) => {
   cur.reasons = [];

   if(cur.weightLoss === 'on') {
      cur.reasons.push('weightLoss');
   }

   if(cur.strength === 'on') {
      cur.reasons.push('strength');
   }

   return [...acc, cur];
}, []);

console.log(normalizedData);

EDIT: Now using map :编辑:现在使用map

const normalizedData = Object.values(payLoadReason).map(item => {
   item.reasons = [];

   if(item.weightLoss === 'on') {
      item.reasons.push('weightLoss');
   }

   if(item.strength === 'on') {
      item.reasons.push('strength');
   }

   return item;
});

console.log(normalizedData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM