方法查找javascript深度数组对象RN react native [英] method find javascript deep array object RN react native

查看:83
本文介绍了方法查找javascript深度数组对象RN react native的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对象的一部分

const category = {
    fr: {
        list: [
            {id: 1, label: 'coucou'},
            {id: 2, label: 'moi'},
            {id: 3, label: 'ici'},
            {id: 4, label: 'maintenant'},
            {id: 5, label: 'demain'},
]}}
const lang = fr;
const anyId = 3;

执行以下操作时,我不知道为什么:

I don't know why when doing the following:

const result = category[lang].list.find(item => item.id === anyId) console.log(result)

抛出以下内容:

//未定义类别[lang] .list.find(item => item.id === anyId)不是 一个函数,或者只是未定义

// undefined category[lang].list.find(item => item.id === anyId) is not a function, or just undefined

.map或.filter的结果相同

same result for .map or .filter

  • console.log(category)不返回错误
  • console.log(category[lang])不返回错误
  • console.log(category[lang].list)不返回错误
  • console.log(category) returns no error
  • console.log(category[lang]) returns no error
  • console.log(category[lang].list) returns no error

,但其他任何内容都会返回错误. 这让我发疯,任何帮助将不胜感激.

but anything else will return an error. It drives me crazy, any help will be highly appreciated.

推荐答案

它适用于mdn沙箱

const category = {
    fr: {
        list: [
            {id: 1, label: 'coucou'},
            {id: 2, label: 'ici'},
            {id: 3, label: 'demain'},
            {id: 4, label: 'matin'},
          ]
    }
};
var lang ='fr';
var catID = 3;
console.log(lang);
console.log(catID);
console.log(category);
console.log(category[lang]);
console.log(category[lang].list);
var found = category[lang].list.find(function(element) {
   return element.id === catID;
});

console.log(found.label); // demain

只需在回调函数内添加一个return, 但它仍然不适用于本机 所以问题仍然存在

just add a return inside the callback function, but it still doesn't work on react-native so the problem remains

这篇关于方法查找javascript深度数组对象RN react native的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆