嗨,我需要在JSON响应中搜索键“ columnName”的所有值,这些值以(...的)特定字符串模式开头(仅开始)。
"IDM_"
对于所有匹配项,我要在每个对象上包括键/值“ 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"
}]
}
答案 0 :(得分:2)
您可以遍历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)
答案 1 :(得分:0)
我认为您可以这样做:
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_') });
答案 2 :(得分:0)
const newResponse = response.columnPermissions
.map(c=> { ...c, 'IDM': c.columnName.includes('IDM_') });