搜索 JSON 部分值匹配并应用 属性 的方法
Method to search JSON for a partial value match and apply a property
您好,我需要在 JSON 响应中搜索键 "columnName" 以特定字符串模式开始(仅开始)的所有值...
"IDM_"
对于所有匹配项,我想在每个对象上包含一个 key/value "IDM":true 并且对于任何不匹配的项目 "IDM":false。
提前致谢。
{
"columnPermissions": [{
"$id": "1474",
"columnName": "IDM_ID"
}, {
"$id": "1475",
"columnName": "IDM_CreateDate"
}, {
"$id": "1476",
"columnName": "IDM_CreatedBy"
}, {
"$id": "1480",
"columnName": "First_Name"
}, {
"$id": "1481",
"columnName": "Last_Name"
}]
}
我想你可以这样做:
let data = JSON.parse({
"columnPermissions": [{
"$id": "1474",
"columnName": "IDM_ID"
}, {
"$id": "1475",
"columnName": "IDM_CreateDate"
}, {
"$id": "1476",
"columnName": "IDM_CreatedBy"
}, {
"$id": "1480",
"columnName": "First_Name"
}, {
"$id": "1481",
"columnName": "Last_Name"
}]
});
data = data.columnPermissions.map(column => Object.assign({}, { 'IDM':
column.columnName.includes('IDM_') });
可以遍历columnPermissions
数组,根据columnName是否startsWith
"IDM_"
设置IDM
属性
const input = {"columnPermissions":[{"$id":"1474","columnName":"IDM_ID"},{"$id":"1475","columnName":"IDM_CreateDate"},{"$id":"1476","columnName":"IDM_CreatedBy"},{"$id":"1480","columnName":"First_Name"},{"$id":"1481","columnName":"Last_Name"}]}
input.columnPermissions.forEach(a =>
a.IDM = a.columnName.startsWith("IDM_")
)
console.log(input)
const newResponse = response.columnPermissions
.map(c=> { ...c, 'IDM': c.columnName.includes('IDM_') });
您好,我需要在 JSON 响应中搜索键 "columnName" 以特定字符串模式开始(仅开始)的所有值...
"IDM_"
对于所有匹配项,我想在每个对象上包含一个 key/value "IDM":true 并且对于任何不匹配的项目 "IDM":false。
提前致谢。
{
"columnPermissions": [{
"$id": "1474",
"columnName": "IDM_ID"
}, {
"$id": "1475",
"columnName": "IDM_CreateDate"
}, {
"$id": "1476",
"columnName": "IDM_CreatedBy"
}, {
"$id": "1480",
"columnName": "First_Name"
}, {
"$id": "1481",
"columnName": "Last_Name"
}]
}
我想你可以这样做:
let data = JSON.parse({
"columnPermissions": [{
"$id": "1474",
"columnName": "IDM_ID"
}, {
"$id": "1475",
"columnName": "IDM_CreateDate"
}, {
"$id": "1476",
"columnName": "IDM_CreatedBy"
}, {
"$id": "1480",
"columnName": "First_Name"
}, {
"$id": "1481",
"columnName": "Last_Name"
}]
});
data = data.columnPermissions.map(column => Object.assign({}, { 'IDM':
column.columnName.includes('IDM_') });
可以遍历columnPermissions
数组,根据columnName是否startsWith
"IDM_"
IDM
属性
const input = {"columnPermissions":[{"$id":"1474","columnName":"IDM_ID"},{"$id":"1475","columnName":"IDM_CreateDate"},{"$id":"1476","columnName":"IDM_CreatedBy"},{"$id":"1480","columnName":"First_Name"},{"$id":"1481","columnName":"Last_Name"}]}
input.columnPermissions.forEach(a =>
a.IDM = a.columnName.startsWith("IDM_")
)
console.log(input)
const newResponse = response.columnPermissions
.map(c=> { ...c, 'IDM': c.columnName.includes('IDM_') });