简体   繁体   English

我如何使用 HTML5 文档的下划线在 javascript 中小写对象值/数据

[英]how can i lowercase object value/data in javascript using underscore for HTML5 document

I have a array of objects ( arr ), each object has 3 properties (Department, Categories, ProductTypes) , the values for these properties are a mixture of upper and lower case.我有一个对象数组( arr ),每个对象有 3 个属性(Department, Categories, ProductTypes) ,这些属性的值是大小写的混合。 I have created a duplicate array of arr and named it arr2 .我创建了一个重复的arr数组并将其命名为arr2 i needed to do a comparison between arr and a search filter (which is already lowercased) so it returns an array of objects back with the matching criteria and assign to arr using:我需要在arr和搜索过滤器(它已经是小写的)之间进行比较,以便它返回一个具有匹配条件的对象数组,并使用以下方法分配给arr

arr=_.where(arr2,filter);

so I'm thinking to iterate through arr2 and lowercase everything in here, as follows:所以我想遍历arr2并将这里的所有内容小写,如下所示:

_.each( arr2, function(item){ //loop around each item in arr2
    _.each( item, function(properties){ //loop around each property in item
        properties=properties.toLowerCase();
        console.log("Item: ",item,"ID: ",properties); 
    });
});

however i get the following output in chrome:但是我在 chrome 中得到以下输出:

Default1.html:109 Item:  Object {Departments: "Dep1", Categories: "Cat1", ProductTypes: "Prod1"} ID:  dep1
Default1.html:109 Item:  Object {Departments: "Dep1", Categories: "Cat1", ProductTypes: "Prod1"} ID:  cat1
Default1.html:109 Item:  Object {Departments: "Dep1", Categories: "Cat1", ProductTypes: "Prod1"} ID:  prod1

How can I change the “ Dep1/Cat1/Prod1 ” values to lowercase?如何将“ Dep1/Cat1/Prod1 ”值更改为小写?

The reason is the when you use for each or for in loops, you get temp variables which are copies of the original and changes will apply only to the copy.原因是当您使用for eachfor in循环时,您会得到临时变量,这些变量是原始变量的副本,而更改仅适用于副本。

Here is working code:这是工作代码:

arr2 = [{ObjectCategories: "Cat1", Departments: "Dep1", ProductTypes: "Prod1"}]

for (var i =0 ; i<arr2.length; i++){
    for (var key in arr2[i]){
        if (key === undefined || arr2[i][key] === undefined) {
            continue;
        }
        console.log(key)
        console.log(arr2[i][key])
        arr2[i][key] = arr2[i][key].toLowerCase();
    };
};
console.log(arr2[0])

For the sake of comparing the following code will not work:为了比较下面的代码将无法正常工作的缘故:

arr2 = [{ObjectCategories: "Cat1", Departments: "Dep1", ProductTypes: "Prod1"}]

for (var item in arr2){
    for (var key in item){
        if (key === undefined || item[key] === undefined) {
            continue;
        }
        console.log(key)
        console.log(item[key])
        item[key] = item[key].toLowerCase();
    };
};
console.log(arr2[0])

You maybe find this interesting.你可能会觉得很有趣。

With some experimenting I managed to solve this, my final code snippet:通过一些实验,我设法解决了这个问题,我的最终代码片段:

var arr2 = [];

_.each( arr, function(item){ //loop around each item in arr
    _.each( item, function(properties, id){ //loop around each property in item
        if (filter[id] == (item[id].toLowerCase())) {
            arr2.push( item );
        }
    });
});

Basically arr2 is declared empty initially, I iterate through arr via underscore for each item object, then iterate though each of the properties within the item object.基本上arr2最初被声明为空,我通过下划线为每个 item 对象遍历arr ,然后遍历 item 对象中的每个属性。 I can then check if the search filter variable (which is already lowercase) equals the item's property value in lowercase, if so then add item object to arr2 object array.然后我可以检查搜索过滤器变量(已经是小写的)是否等于小写项目的属性值,如果是,则将项目对象添加到arr2对象数组。 Simple!简单的!

There might be a more shorter function available in underscore to tidy this up even more.下划线中可能有一个更短的函数可以更进一步地整理它。

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

相关问题 如何使用HTML5中的sessionStorage概念在Javascript中打印对象数据 - How to print object data in Javascript using sessionStorage concepts in HTML5 如何为元素分配HTML5数据值? - How can I assign an HTML5 data value to an element? 带有下划线js的数组中对象值的小写 - Lowercase of object value in array with underscore js 如何使用HTML5缓存大图像数据 - How can i cache large image data using HTML5 如何使用JavaScript检测文档中是否存在HTML5有效的doctype? - How to detect if an HTML5 valid doctype is present in the document using JavaScript? 如何使用 Javascript 将数组中的元素插入到 HTML 文档中? - How can I insert elements in an array to a HTML document using Javascript? 如何使用underscore.js找到javascript数组中存在的给定值? - How can I find a given value exists in javascript array using underscore.js? 如何使用 JavaScript 测试字符串中的字母是大写还是小写? - How can I test if a letter in a string is uppercase or lowercase using JavaScript? 如何检索 HTML 表单值并将其放入 JavaScript object? - How can I retrieve an HTML form value and put it in a JavaScript object? 我可以使用javascript调用原生HTML5输入工具提示吗? - Can I invoke native HTML5 input tooltip using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM