简体   繁体   English

在 JavaScript 中循环遍历数组中每个对象的每个属性值

[英]Looping through each property values of each object in an array in JavaScript

How to loop through each object property value inside an array?如何遍历数组中的每个对象属性值? I want to sanitize all values before rendering it in the front-end.我想在前端渲染之前清理所有值。

sampleData = [
    { "Name": "<p>John &nbsp;</p>", "Age": 23, "Student": false },
    { "Name": "Bruno", "Age": 20, "Student": true },
    { "Name": "David &nbsp;", "Age": 30, "Student": false },
];

Here is the old code:这是旧代码:

this.sampleData.forEach((tableRow) => {
    let column = Object.entries(tableRow);
    column.forEach((value) => {
        let cell;
        cell = value[1];
        if ((typeof cell !== 'boolean') && (cell !== null) && (typeof cell !== 'number')) {
            cell = this.sanitizeString(cell);
        }
        console.log(cell);


    });

});

PS I am using Papa parse for parsing .csv data and already have the sanitation method. PS 我正在使用 Papa parse 来解析 .csv 数据并且已经有了卫生方法。

For those who might face the same task as above, I solved it using this code:对于那些可能面临与上述相同任务的人,我使用以下代码解决了它:

this.dataPreview = this.sanitizedArray(result.data);

sanitizedArray(row) {
  Object.keys(row).forEach((key) => {
  // Sanitize each field in this scope
  return Object.keys(row[key]).forEach((column) => {
    if ( (typeof row[key][column] !== 'boolean') && (row[key][column] !== null) &(typeof row[key][column] !== 'number') ) {
    row[key][column] = this.sanitizeString(row[key][column]);
  }
  });
  // End of sanitation
});
return row;

} }

or use my friend suggestion:或使用我朋友的建议:

const updatedSampleData = sampleData.map((data) => ({ ...data, property: newValue }));

Thanks谢谢

I think Sandhands could be a great solution.我认为Sandhands可能是一个很好的解决方案。 Here's an example of how you could validate all of that data at once in a short amount of code这是一个示例,说明如何在少量代码中一次验证所有数据

const {sanitize} = require('sandhands')

sanitize(sampleData, [{Name: {_: String, trimmed: true}, Age: {_: Number, min: 1}, Student: Boolean}]) // throws if the sample data is not properly formatted

Disclosure: I wrote Sandhands, but it's free and open-source披露:我写了 Sandhands,但它是免费和开源的

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

相关问题 循环遍历数组并将每个元素设置为 object 的属性会导致顺序混乱 - Looping through an array and setting each element as a property of an object messes with the order 带有数组的 Angular Javascript 对象,循环遍历并获取每个数组的长度? - Angular Javascript object with arrays, looping through and getting length of each array? Javascript“ .each”没有遍历数组 - Javascript “.each” isn't looping through array 遍历JavaScript对象并在每次迭代中更改表内容 - Looping through JavaScript Object and changing table content on each iteration 循环遍历多维数组以迭代javascript中的每个项目 - Looping Through Multidimensional Array to iterate each item in javascript 循环遍历 javascript 数组以检查每个项目的某些子元素 - Looping through a javascript array to check each item for certain children elements 循环遍历数组以向每个对象添加ko.computed - Looping through array to add ko.computed to each object 遍历对象数组并返回每个 object 的键和值 - Looping through array of objects and return the key and value for each object javascript:遍历嵌套对象/数组并添加属性 - javascript: looping through nested object/array and adding property JavaScript将数组中每个对象中的所有值求和? - JavaScript sum all of the values in each object in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM