【问题标题】:javascript function to search for object property and return value用于搜索对象属性和返回值的 javascript 函数
【发布时间】:2018-11-23 11:44:11
【问题描述】:

我有一个字符串: const phrase = "there is a blue bird in the forest";

还有一个对象:

const color = {
'blue': 20,
'red': 10,
'yellow': 5
};

我想编写一个 Javascript 函数来检查字符串是否包含颜色对象的任何属性,如果是,则返回匹配属性的值,因此在上面的示例中,它将返回 20。

我在用 Lodash,不知道怎么写这个函数(_.some, _.find?)

【问题讨论】:

  • 欢迎来到 Stack Overflow :) 你能提供你已经尝试过的吗?

标签: javascript lodash


【解决方案1】:

如果需要获取字符串中所有颜色的总值,可以使用Array.reduce()(或者lodash的_.reduce())。将短语改为小写,用空格分割,归约,取颜色值的总和(或者换成0):

const color = {
  'blue': 20,
  'red': 10,
  'yellow': 5
};

const getColorsValue = (p) =>
  p.toLowerCase()
   .split(/\s+/)
   .reduce((s, w) => s + (color[w] || 0), 0);

console.log(getColorsValue('there is a blue bird in the forest')); // 20

console.log(getColorsValue('there is a blue bird in the red forest')); // 30

【讨论】:

    【解决方案2】:

    这对你很有用,看看这个或在下面找到代码:https://dustinpfister.github.io/2017/09/14/lodash-find/

    var db_array = [
    
    {
        name : 'Dave',
        sex : 'male',
        age : 34
    },
    
    {
        name: 'Jake',
        sex : 'male',
        age : 22
    },
    
    {
        name :'Jane',
        sex : 'female',
        age : 27
    }
    
    
    ],
    
    // find dave
    q = _.find(db_array, {name:'Dave'});
    
    console.log(q); // {name:'Dave',sex:male,age:34}
    

    【讨论】:

      【解决方案3】:

      试试Underscore.js Library_.where(list, properties)

      【讨论】:

        【解决方案4】:

        这应该对你有帮助!

        const phrase = "there is a blue bird in the forest";
        const color = { 'blue': 20, 'red': 10, 'yellow': 5 };
        
        const phraseValues = phrase.split(' ');
        const colorValues = Object.keys(color)
        
        const isKeyPresent = !!_.intersection(phraseValues , colorValues).length
        

        【讨论】:

          【解决方案5】:

          我们可以利用 JavaScript 的 Object.keys().find()

          来实现这一点

          const phrase = "there is a blue bird in the forest";
          const color = { 'blue': 20, 'red': 10, 'yellow': 5 };
          
          const result = color[Object.keys(color).find(v => phrase.indexOf(v) !== -1)];
          
          console.log(result); // 20

          【讨论】:

            【解决方案6】:

            使用js:

            const phrase = "there is a blue bird in the forest";
                
            const color = { 'blue': 20, 'red': 10, 'yellow': 5 };
                
            let key = Object.keys(color).find(color => phrase.includes(color));
                
            if(key) console.log(color[key]);

            【讨论】:

              【解决方案7】:

              您也可以使用Array.flatMapArray.split

              const phrase = "there is a blue bird in the forest";
              
              const color = {
              'blue': 20,
              'red': 10,
              'yellow': 5
              };
              
              let res = phrase.split(' ').flatMap(d => color[d] || [])
              
              console.log(res[0] || 'No color is present')

              【讨论】:

                【解决方案8】:

                您还可以在颜色上的Array.reduce 内使用String.replace 处理程序来遍历每种颜色并计算最终总和。

                const data = "blue and red bird with blue feathers"
                const color = { 'blue': 20, 'red': 10, 'yellow': 5 }
                
                const result = Object.keys(color).reduce((r, k) => 
                  (data.replace(new RegExp(k, 'g'), () => r += color[k]), r), 0)
                
                console.log(result)  // 50 since it has 2 "blue" and 1 "red"

                【讨论】:

                  猜你喜欢
                  • 2020-02-03
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-06-14
                  • 1970-01-01
                  • 2012-07-18
                  • 2019-03-08
                  • 1970-01-01
                  相关资源
                  最近更新 更多