3 回答

TA貢獻1850條經驗 獲得超11個贊
將 Intl.Collator 與 一起使用sensitivity: 'base',這意味著等效字母的權重相同。效率比toLower. 這是一個非常高效的對象比較器,它不需要 toLower 或 JSON.stringify 并且在屬性為字符串時忽略大小寫。
const compareStr = new Intl.Collator(undefined, { sensitivity: 'base' }).compare;
const compare = (obj1, obj2) =>
Array.isArray(obj1)
? Array.isArray(obj2) && obj1.length === obj2.length && obj1.every((item, index) => compare(item, obj2[index]))
: obj1 instanceof Date
? obj2 instanceof Date && obj1.getDate() === obj2.getDate()
: obj1 && typeof obj1 === 'object'
? obj2 && typeof obj2 === 'object' &&
Object.getOwnPropertyNames(obj1).length === Object.getOwnPropertyNames(obj2).length &&
Object.getOwnPropertyNames(obj1).every(prop => compare(obj1[prop], obj2[prop]))
: typeof obj1 === 'string' && typeof obj2 === 'string'
? compareStr(obj1, obj2) === 0
: obj1 === obj2;
console.log(compare({ prop: 'a' }, { prop: 'A' }));
console.log(compare(['b'], ['B']));
console.log(compare('a', 'A'));
console.log(compare('B', 'b'));
console.log(compare('B', 'a'));
console.log(compare('Mismatched case', 'MisMatched Case'));

TA貢獻1817條經驗 獲得超6個贊
使用下面的方法僅將管道符號左側的“內容”轉換為小寫。
//Change to lower case from left side of | sign for content
jsonObject1.forEach(function(value, index) {
if (value.inclusionOptions) {
value.inclusionOptions.forEach(function(value2, index2) {
var tokens = value2.content.split('|');
value2.content = tokens[0].toLowerCase() + "|" + tokens[1];
});
}
});
console.log(jsonObject1);

TA貢獻1871條經驗 獲得超13個贊
您可以將響應作為字符串并比較每個的小寫版本:
function isSame(res1, res2) {
return JSON.stringify(res1).toLowerCase() == JSON.stringify(res2).toLowerCase()
}
console.log( isSame({"DATA": "X"}, {"data": "x"}) )
添加回答
舉報