为什么与React Native中的相同本地对象相比,API JSON对象返回false ?



我想比较来自API的对象与本地对象,但是当我想在控制台中比较它时它显示为false。对象是一样的。为什么比较相同的对象返回false?我怎样才能得到真实?

const [QuizCategoriesData, setQuizCategoriesData] = useState([])
const getData = async () => {
const url = `https://eu-central-1.aws.data.mongodb-api.com/app/application-0-ekvws/endpoint/zdalneAPIHurraFajnie?secret=sekret&arg1=Expert_1`;
const res = await fetch(url);
const data = await res.json();
const filterCategory = data.filter(item=> item.category === 'Mentalność bogacenia się')
setQuizCategoriesData(filterCategory[0].data)
};
useEffect(() => {
getData();
}, []);

const APIObject = QuizCategoriesData;
let arr2 = [{"correct_option": "Jupiter",
"difficulty": "easy", 
"options": ["Jupiter", "Saturn", "Neptune", "Mercury"], 
"question": "What’s the biggest planet in our solar system?"} ];
console.log('api', APIObject[0])
console.log('local', arr2[0])
console.log('Comprasion:',JSON.stringify(APIObject[0]) === JSON.stringify(arr2[0])) //console returns false

下面是控制台

的结果

已解决:比较函数deep-diff对我有效。https://www.npmjs.com/package/deep-diff

我仍然不知道为什么API对象不同,但重要的是它现在可以工作了

这是应用类别筛选器后https调用的结果。来自https输出的数据部分比您比较的数据部分要大得多。此外,我建议您使用一些比较函数而不是stringify ..这里有一个如何在React Native中比较对象的例子

{
"_id": "62a3492b2ce9f36281fc83a0",
"category": "Mentalność bogacenia się",
"data": [{
"question": "What’s the biggest planet in our solar system?",
"options": ["Jupiter", "Saturn", "Neptune", "Mercury"],
"correct_option": "Jupiter",
"difficulty": "easy"
}, {
"question": "What attraction in India is one of the famus in the world?",
"options": ["Chand Minar", "Taj Mahal", "Stadium"],
"correct_option": "Taj Mahal",
"difficulty": "medium"
}, {
"question": "What land animal can open its mouth the widest?",
"options": ["Alligator", "Crocodile", "Baboon", "Hippo"],
"correct_option": "Hippo",
"difficulty": "easy"
}, {
"question": "What is the largest animal on Earth?",
"options": ["The African elephant", "The blue whale", "The sperm whale", "The giant squid"],
"correct_option": "The blue whale",
"difficulty": "hard"
}, {
"question": "What is the only flying mammal?",
"options": ["The bat", "The flying squirrel", "The bald eagle", "The colugo"],
"correct_option": "The bat",
"difficulty": "medium"
}, {
"question": "What’s the biggest planet in our solar system?",
"options": ["Jupiter", "Saturn", "Neptune", "Mercury"],
"correct_option": "Jupiter",
"difficulty": "hard"
}, {
"question": "What attraction in India is one of the famus in the world?",
"options": ["Chand Minar", "Taj Mahal", "Stadium"],
"correct_option": "Taj Mahal",
"difficulty": "medium"
}, {
"question": "What land animal can open its mouth the widest?",
"options": ["Alligator", "Crocodile", "Baboon", "Hippo"],
"correct_option": "Hippo",
"difficulty": "easy"
}, {
"question": "What is the largest animal on Earth?",
"options": ["The African elephant", "The blue whale", "The sperm whale", "The giant squid"],
"correct_option": "The blue whale",
"difficulty": "medium"
}, {
"question": "What is the only flying mammal?",
"options": ["The bat", "The flying squirrel", "The bald eagle", "The colugo"],
"correct_option": "The bat",
"difficulty": "medium"
}]
}

尝试使用"=="操作符

相关内容

  • 没有找到相关文章

最新更新