gpt4 book ai didi

javascript - 使用下划线递归探索对象并转换字段类型

转载 作者:行者123 更新时间:2023-12-02 15:29:55 25 4
gpt4 key购买 nike

我正在使用一些自定义 JSON 对象。

我正在尝试探索整个对象,并更改字段类型。

例如,real_id或clean_id甚至温度都需要转换为Integer。

为了解决这个问题,我开始制作一个非常简单的函数:

var reg = /^\d+$/;
_.each(myObject, function(val, key) {
myObject[key] = reg.test(val) ? parseInt(val) :val;
});

我有几个需求:

  1. 它将在第一级逐一检查每个字段,但如果 myObject 具有本身包含对象或对象数组的元素,则我的 _.each 将不会'不要去探索它们。
  2. 目前,它可以通过正则表达式检测整数,并将它们转换为整数,但如果我需要转换 float 怎么办?
  3. 如果我发现 null,我需要将其设置为 0

就我下面的例子来说,

jSON 对象示例:

myObject = {
"real_name": "Test",
"maker": "Jean-Paul",
"company": "",
"real_id": "646402",
"clean_id": "152691",
"year": 2013,
"type": "Red real",
"available_for_order": 0,
"level": null,
"bio": null,
"temperature": "15",
"potential": "3",
"country_code": "USA",
"coord_lng": "2.7014349999999",
"coord_lat": "42.717317",
"real_description": [
{
"comment": "Real good stuff",
"lang": "it",
"video_end": null,
"video_id": null,
"video_start": null,
"video_url": null
},
{
"comment": "Awesome and cheap stuff",
"lang": "en",
"video_end": null,
"video_id": null,
"video_start": null,
"video_url": null
}
],
"full_description": [
{
"description": "Long description",
"lang": "fr"
},
{
"description": "",
"lang": "en"
}
],
"list": [
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 0,
"quantity": 0,
"retail_price": 0,
"real_id": "656732",
"year": "1"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 0,
"quantity": 0,
"retail_price": 0,
"real_id": "330381",
"year": "2008"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 20,
"quantity": 0,
"retail_price": 0,
"real_id": "11453216",
"year": "2010"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 0,
"quantity": 0,
"retail_price": 0,
"real_id": "11497420",
"year": "2011"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "1",
"currency_code": "EUR",
"price": 10,
"quantity": 0,
"retail_price": 0,
"real_id": "11506715",
"year": "2012"
},
{
"available_for_order": "1",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 10,
"quantity": 0,
"retail_price": 0,
"real_id": "11458207",
"year": "2013"
}
],
"check_list": [
"3",
"8",
"9",
"10",
"14"
],
"image_src": "791330_31odFZZoM2_JqkCPJGHXa_4500x1000.jpeg"
}

非常感谢您的帮助

最佳答案

在我看来,你需要的是一个简单的递归示例,不需要 Lodash...

注意:您使用的正则表达式仅匹配以一次或多次出现的数字开头和结尾的字符串...因此,您应该使用 Number 来转换值,而不是使用 parseInt 或 parseFloat!

在 Javascript 中,Number 是 Number 类型的构造函数,但是,如果在没有 new 运算符的情况下调用,则会执行基本的强制转换。当转换不满足时,它返回 NaN,因此,使用 or 运算符我们可以保留原始值!

希望有帮助...

var myObject = {
"real_name": "Test",
"maker": "Jean-Paul",
"company": "",
"real_id": "646402",
"clean_id": "152691",
"year": 2013,
"type": "Red real",
"available_for_order": 0,
"level": null,
"bio": null,
"temperature": "15",
"potential": "3",
"country_code": "USA",
"coord_lng": "2.7014349999999",
"coord_lat": "42.717317",
"real_description": [
{
"comment": "Real good stuff",
"lang": "it",
"video_end": null,
"video_id": null,
"video_start": null,
"video_url": null
},
{
"comment": "Awesome and cheap stuff",
"lang": "en",
"video_end": null,
"video_id": null,
"video_start": null,
"video_url": null
}
],
"full_description": [
{
"description": "Long description",
"lang": "fr"
},
{
"description": "",
"lang": "en"
}
],
"list": [
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 0,
"quantity": 0,
"retail_price": 0,
"real_id": "656732",
"year": "1"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 0,
"quantity": 0,
"retail_price": 0,
"real_id": "330381",
"year": "2008"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 20,
"quantity": 0,
"retail_price": 0,
"real_id": "11453216",
"year": "2010"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 0,
"quantity": 0,
"retail_price": 0,
"real_id": "11497420",
"year": "2011"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "1",
"currency_code": "EUR",
"price": 10,
"quantity": 0,
"retail_price": 0,
"real_id": "11506715",
"year": "2012"
},
{
"available_for_order": "1",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 10,
"quantity": 0,
"retail_price": 0,
"real_id": "11458207",
"year": "2013"
}
],
"check_list": [
"3",
"8",
"9",
"10",
"14"
],
"image_src": "791330_31odFZZoM2_JqkCPJGHXa_4500x1000.jpeg"
};


function recurse(obj) {
for(var key in obj) {
if(!obj.hasOwnProperty(key)) { continue; }

if('object' === typeof obj[key]) {
recurse(obj[key]);
} else {
console.log(key, obj[key], typeof obj[key], ' ==> ', typeof (Number(obj[key]) || obj[key]));
obj[key] = Number(obj[key]) || obj[key];
}

}
}

recurse(myObject);

关于javascript - 使用下划线递归探索对象并转换字段类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33400395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com