【问题标题】:method find javascript deep array object RN react native方法find javascript deep array object RN react native
【发布时间】:2018-02-23 15:49:39
【问题描述】:

这是我的对象的一部分

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;

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

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

抛出以下内容:

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

.map 或 .filter 的结果相同

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

但其他任何事情都会返回错误。 它让我发疯,任何帮助将不胜感激。

【问题讨论】:

  • const lang = 'fr' not lteral fr 通过将fr 不包含在引号中,您的代码期望fr 已经定义(因此在运行时出现undefined 错误):- )
  • is not a function, or just undefined --> 那么是第一个错误还是第二个错误?它是未定义的,但已定义,只是不是一个函数??
  • console.log(category[lang]) returns no error,但它显示了什么?
  • 您的代码可以将fr 用引号括起来,但您需要确定这是您想要的。变量fr这里const lang = fr;是另一个声明的变量?

标签: javascript object react-native find


【解决方案1】:

使用const lang = "fr" 而不是const lang = fr,因为fr 是一个未定义的变量,而"fr" 是一个字符串。所以你会得到category["fr"] 而不是category[fr]

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;

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

console.log(result)

【讨论】:

  • 我认为这不是问题
  • 我认为是的,因为如果你使用“fr”,代码就可以工作。
  • 如果这是问题所在,错误将是fr is not defined
  • 好吧,运行我的 sn-p。如果您使用"fr",它可以工作。我还能说什么
  • 当然可以,但这无济于事。想象一下fr 是代码的另一部分中声明的变量,并导致 OP 面临该问题。
【解决方案2】:

您需要category.fr 而不仅仅是fr,因为变量fr 不存在。

现在lang 包含您的fr 对象,您可以简单地在lang.list 上执行.find(),如下所示:

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

// Fill param from a variable, or anything, as long as it's a string:
const param = 'fr';
// Use brackets here, as you want `category.fr` and not `category.param`:
const lang = category[param];
//Or you can simply use:
//const lang = category.fr; //If this is not a parameter, or user input

const anyId = 3;

console.log(lang);

console.log(lang.list.find(item => item.id === anyId));

【讨论】:

  • 然后,如果有多个fr,您如何获得参数语言?比如ende
【解决方案3】:

它适用于 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

只需在回调函数中添加一个返回, 但它仍然不适用于 react-native 所以问题仍然存在

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 2022-12-01
    • 2019-06-18
    • 2012-02-29
    • 2021-02-04
    相关资源
    最近更新 更多