js 处理数据结构
数据如下:想要的数据格式是数组PriceList:后面跟着每一项的key和value,priceList的长度不固定,每一个括号等于一条数据,应该如何处理,感谢各位
[PriceList(id=2fa6ee62-d0fe-4e78-a84e-c228e3344f4d,
adjustItemsId=e061aa19-59e6-4bab-bb4a-1d4aa9f6e9ef,
dimensionId=null,
dimensionCode=null,
dimensionName=null,
dimensionType=0,
productName=null,
productCode=null,
product=95832d39-9856-4a7d-9ec8-3baeb4f427a7,
SKUName=null,
SKUCode=null,
SKU=97580289-6108-4c29-af08-85a83abcae30,
measureUnit=2f83bc78-34e6-40cf-9745-1aa50bf783db,
measureUnitName=null,
price=322.00000000,
startDate=2022-04-01 08:00:00.0,
endDate=null,
tenant=O202202081613002675764,
tenantName=null,
tenantCode=null),
PriceList
(id=7d338f82-0fd7-4fe4-8c26-241786a92082,
adjustItemsId=a358ced1-2c77-45ed-8aa6-c539cc412c9c,
dimensionId=null,
dimensionCode=null,
dimensionName=null,
dimensionType=0,
productName=null,
productCode=null,
product=95832d39-9856-4a7d-9ec8-3baeb4f427a7,
SKUName=null,
SKUCode=null,
SKU=a89855d1-a6ad-4f31-b109-4e42e0367bd8,
measureUnit=2f83bc78-34e6-40cf-9745-1aa50bf783db,
measureUnitName=null,
price=323.00000000,
startDate=2022-04-01 08:00:00.0,
endDate=null,
tenant=O202202081613002675764,
tenantName=null,
tenantCode=null)]
想要的数据格式:
PriceList:[{
id:"2fa6ee62-d0fe-4e78-a84e-c228e3344f4d",
adjustItemsld:"e061aa19-59e6-4bab-bb4a-1d4aa9f6e9ef",
dimensionId:null,
},
{
id:"7d338f82-0fd7-4fe4-8c26-241786a92082",
....
....
}
]
回答:
const data = `...` // 题主给的字符串const regex = /\([\s\S]+?\)/g;
const m = data.match(regex);
const list = (m ?? [])
.map(s => s.slice(1, -1))
.map(s => s.split(",").map(s => s.trim().split("=", 2)))
.map(entries => Object.fromEntries(entries));
console.log(list);
回答:
//把原字符串中的PriceList当作方法,括号里面的当参数,用eval直接执行;let str = "[PriceList(id=2fa6ee62-d0fe-4e78-a84e-c228e3344f4d, adjustItemsId=e061aa19-59e6-4bab-bb4a-1d4aa9f6e9ef, dimensionId=null, dimensionCode=null, dimensionName=null, dimensionType=0, productName=null, productCode=null, product=95832d39-9856-4a7d-9ec8-3baeb4f427a7, SKUName=null, SKUCode=null, SKU=97580289-6108-4c29-af08-85a83abcae30, measureUnit=2f83bc78-34e6-40cf-9745-1aa50bf783db, measureUnitName=null, price=322.00000000, startDate=2022-04-01 08:00:00.0, endDate=null, tenant=O202202081613002675764, tenantName=null, tenantCode=null), PriceList(id=7d338f82-0fd7-4fe4-8c26-241786a92082, adjustItemsId=a358ced1-2c77-45ed-8aa6-c539cc412c9c, dimensionId=null, dimensionCode=null, dimensionName=null, dimensionType=0, productName=null, productCode=null, product=95832d39-9856-4a7d-9ec8-3baeb4f427a7, SKUName=null, SKUCode=null, SKU=a89855d1-a6ad-4f31-b109-4e42e0367bd8, measureUnit=2f83bc78-34e6-40cf-9745-1aa50bf783db, measureUnitName=null, price=323.00000000, startDate=2022-04-01 08:00:00.0, endDate=null, tenant=O202202081613002675764, tenantName=null, tenantCode=null)]"
let result = [];
let fnStr = str.replace(/\(|\)/g, function (i) {
return i == "(" ? "('" : "')"
})
let fn = fnStr.slice(1, fnStr.length - 1);
function PriceList(arr) {
let o = arr.split(',').map(item => item.trim().split('='));
result.push(Object.fromEntries([...o]));
}
eval(fn);
console.log(result)
回答:
function PriceList(...arguments) { const obj = {
id:[...arguments][0],
adjustItemsld:[...arguments][1],
dimensionId:[...arguments][2],
}
return obj
}
const data = [
PriceList(id = "2fa6ee62-d0fe-4e78-a84e-c228e3344f4d",
adjustItemsId = "e061aa19-59e6-4bab-bb4a-1d4aa9f6e9ef",
dimensionId = null,
dimensionCode = null,
dimensionName = null,
dimensionType = 0,
productName = null,
productCode = null,
product = "95832d39-9856-4a7d-9ec8-3baeb4f427a7",
SKUName = null,
SKUCode = null,
SKU = "97580289-6108-4c29-af08-85a83abcae30",
measureUnit = "2f83bc78-34e6-40cf-9745-1aa50bf783db",
measureUnitName = null,
price = 322.00000000,
startDate = "2022-04-01 08:00:00.0",
endDate = null,
tenant = "O202202081613002675764",
tenantName = null,
tenantCode = null),
PriceList(id = "7d338f82-0fd7-4fe4-8c26-241786a92082",
adjustItemsId = "a358ced1-2c77-45ed-8aa6-c539cc412c9c",
dimensionId = null, dimensionCode = null, dimensionName = null,
dimensionType = 0, productName = null, productCode = null,
product = "95832d39-9856-4a7d-9ec8-3baeb4f427a7",
SKUName = null, SKUCode = null,
SKU = "a89855d1-a6ad-4f31-b109-4e42e0367bd8",
measureUnit = "2f83bc78-34e6-40cf-9745-1aa50bf783db",
measureUnitName = null, price = 323.00000000,
startDate = "2022-04-01 08:00:00.0", endDate = null,
tenant = "O202202081613002675764", tenantName = null,
tenantCode = null)
]
console.log(data)
[{"id":"2fa6ee62-d0fe-4e78-a84e-c228e3344f4d","adjustItemsld":"e061aa19-59e6-4bab-bb4a-1d4aa9f6e9ef","dimensionId":null},{"id":"7d338f82-0fd7-4fe4-8c26-241786a92082","adjustItemsld":"a358ced1-2c77-45ed-8aa6-c539cc412c9c","dimensionId":null}]
以上是 js 处理数据结构 的全部内容, 来源链接: utcz.com/p/937384.html