简体   繁体   中英

javascript function to search for object property and return value

I have a string: const phrase = "there is a blue bird in the forest";

and an object:

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

I want to write a Javascript function that checks if the string contains any of the property of the color object and, if so, return the value for the matched property, so in the example above, it will return 20.

I'm using Lodash and I can't figure out how to write this function (_.some, _.find?)

If you need to get the total value of all colors in a string, you can use Array.reduce() (or lodash's _.reduce() ). Change the phrase to lower case, split it by spaces, reduce, and take sum the value of the color (or 0 for other words):

 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 

This will be useful for you, check this out or find code below: 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}

Using 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]); 

We can achieve this utilising JavaScript's Object.keys() and .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 

Try Underscore.js Library . _.where(list, properties)

This should help you!

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

You can use Array.flatMap and Array.split as well

 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') 

You could also use the String.replace handler inside an Array.reduce on the colors to go though each and calculate the final sum.

 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" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM