有沒有更好的方法來檢查 JSON 資料中的空資料?
這是我擁有的 JSON 型別,有時資料丟失,這是正常的:
{
"sumRatings": "Private info",
"phoneNum": "Private info",
"firstname": "Private info",
"email": "Private info",
"fullname": "Private info",
"talentType": "Private info",
"ratingCount": "Private info",
"creationTime": "Private info",
"lastname": "Private info",
"currentPosition": "Private info",
"rgpd": "Private info",
"bio": "Private info",
"company": "Private info",
"whatsapp": "Private info",
"emptyComs": "Private info",
"id": "Private info"
}
這是我的做法:
for (const item of allDocs) {
var generalData = {
bio: item.bio == undefined ? null : item.bio,
fullname: item.fullname == undefined ? null : item.fullname,
company: item.company == undefined ? null : item.company,
position: item.position == undefined ? null : item.position,
profilImg: item.profilImg == undefined ? null : item.profilImg,
talentType: item.talentType == undefined ? null : item.talentType,
rating: item.rating == undefined ? null : item.rating,
contacts: {
gmeets: item.gmeets == undefined ? null : item.gmeets,
zoom: item.zoom == undefined ? null : item.zoom,
phone: item.phone == undefined ? null : item.phone,
},
skills: item.skills == undefined ? null : item.skills,
email: item.email == undefined ? null : item.email,
firstname: item.firstname == undefined ? null : item.firstname,
lastname: item.lastname == undefined ? null : item.lastname,
phone: item.phoneNum == undefined ? null : item.phoneNum,
ratingCount:
item.ratingCount == undefined ? null : item.ratingCount,
sumRatings: item.sumRatings == undefined ? null : item.sumRatings,
currentPosition:
item.currentPosition == undefined ? null : item.currentPosition,
creationTime:
item.creationTime == undefined ? null : item.creationTime,
rgpd: item.rgpd == undefined ? null : item.rgpd,
emptyComs: item.emptyComs == undefined ? null : item.emptyComs,
};
console.log(generalData);
}
我想知道是否有辦法避免... == undefined ? null : ...
放在每一行。也許一個函式會獲取專案屬性,檢查它們是否未定義,如果未定義則回傳“null”,如果未定義則回傳實際值。
uj5u.com熱心網友回復:
從上面的評論...
“首先,如果物件的任何屬性具有
undefined
值,則該物件永遠不會從有效的 JSON資料中檢索到,因為該undefined
值不是 JSON 符合編碼的一部分。其次,檢查樹狀結構的所有(嵌套)條目通常由一種遞回方法。第三,Object.entries
回傳所有物件自己的可列舉鍵值對。
function recursivelyNullifyUndefinedValues(obj) {
Object
.entries(obj)
.forEach(([key, value]) => {
if (!!value && (typeof value === 'object')) {
recursivelyNullifyUndefinedValues(value);
} else if (value === undefined) {
obj[key] = null;
}
});
return obj;
}
const sampleData = {
talentType: "foo",
rating: "bar",
contacts: {
gmeets: undefined,
zoom: undefined,
phone: "baz",
},
};
console.log('before ...', { sampleData });
console.log('after ...', { returnValue: recursivelyNullifyUndefinedValues(sampleData) });
console.log('after ...', { sampleData });
.as-console-wrapper { min-height: 100%!important; top: 0; }
編輯
由于 OP 似乎從某種平面/普通資料配置中創建了某種更結構化的資料項,因此還有其他技術/工具,例如解構賦值/物件解構、解構賦值/默認值和解構賦值/休息語法
function recursivelyNullifyUndefinedValues(obj) {
Object
.entries(obj)
.forEach(([key, value]) => {
if (!!value && (typeof value === 'object')) {
recursivelyNullifyUndefinedValues(value);
} else if (value === undefined) {
obj[key] = null;
}
});
return obj;
}
function createNullifiedStructuredDataFromFlatConfig(config) {
const {
// handle nullification already
// via destructuring default values.
gmeets = null, zoom = null, phone = null, ...rest
} = config;
// take care of whatever was left with the `...rest` data.
return recursivelyNullifyUndefinedValues({
contacts: {
gmeets,
zoom,
phone,
},
...rest,
});
}
const flatItemData = {
talentType: 'foo',
rating: 'bar',
gmeets: undefined,
zoom: undefined,
phone: 'baz',
skills: undefined,
email: 'buzz',
}
const structuredSampleData =
createNullifiedStructuredDataFromFlatConfig(flatItemData);
console.log({ flatItemData, structuredSampleData });
.as-console-wrapper { min-height: 100%!important; top: 0; }
uj5u.com熱心網友回復:
我認為最有用的是一個函式,它接受一個輸出物件規范,描述其元素在原始結構中的位置。遞回撰寫很簡單。此版本允許您為輸出中的任何位置指定輸入??的屬性,并null
為缺少的位置傳遞:
const restructure = (spec) => (o) =>
Object .fromEntries (Object .entries (spec) .map (([k, v]) => [
k,
Object (v) === v ? restructure (v) (o) : v in o ? o [v] : null
]))
const spec = {
bio: 'bio',
fullname: 'fullname',
company: 'company',
position: 'position',
profilImg: 'item',
talentType: 'talentType',
rating: 'rating',
contacts: {
gmeets: 'gmeet',
zoom: 'zoom',
phone: 'phone',
},
skills: 'skills',
email: 'email',
firstname: 'firstname',
lastname: 'lastname',
phone: 'phoneNum',
ratingCount: 'ratingCount',
sumRatings: 'sumRatings',
currentPosition: 'currentPosition',
creationTime: 'creationTime',
rgpd: 'rgpd',
emptyComs: 'emptyComs',
}
const o = {sumRatings: "Private info", phoneNum: "Private info", firstname: "Private info", email: "Private info", fullname: "Private info", talentType: "Private info", ratingCount: "Private info", creationTime: "Private info", lastname: "Private info", currentPosition: "Private info", rgpd: "Private info", bio: "Private info", company: "Private info", whatsapp: "Private info", emptyComs: "Private info", id: "Private info"}
console .log (restructure (spec) (o))
.as-console-wrapper {max-height: 100% !important; top: 0}
如果我們想重組一個這樣的專案陣列,那就是
const newItems = items .map (restructure (spec))
我們可能需要考慮兩個有用的擴展:
這可能不能很好地處理陣列。我們可以擴展它來做到這一點。
我們可能真的想重新格式化嵌套物件的資料,而不僅僅是平面物件。因此,最好有諸如
bio: 'additionalInfo.personal.biography'
或 之類的條目,以便獲得最大的靈活性,bio: ['additionalInfo', 'personal', 'biography']
以便bio
獲取我們輸入物件中屬性值biography
的屬性值的personal
屬性值,再次默認為。additionalInfo
null
這些都不難。我們可以將它們作為練習留給讀者。
uj5u.com熱心網友回復:
x == undefined ? null : x
您可以將這些部分簡化為x ?? null
并將其包裝在一個函式中,但您可能希望查看像yup這樣的模式驗證庫。
uj5u.com熱心網友回復:
您也可以簡單地使用布林值或來簡寫檢查。例如。
...
var generalData = {
bio: item.bio || null,
fullname: item.fullname || null,
company: item.company || null,
...
如果 x 未定義或為 null,則運算式(x || null)
的計算結果為 null。否則,它的計算結果為 x。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/470059.html
標籤:javascript 目的 递归 数据结构 类型检查
上一篇:在Ocaml中跟蹤嵌套遞回
下一篇:搜索深度嵌套陣列以更新物件