Is it possible to change a property inside an element and return the element itself?

I have this code in my application

var name = 'TesT'; 
var nameLowercase = name && name.toLowerCase (), 
    namesToTest = ['asd', 'asd1', 'TesT'];

var findCorrectName = R.pipe(
    R.map(R.toLower),     
    R.any(R.equals(nameLowercase))
);

var hasFoundName = findCorrectName (namesToTest);

return hasFoundName;

But now I need to search the list of objects, and the "name" property should be placed in lowercase. When an object is found that matches my search, I need to get it.

I read the documentation but could not find a solution.

var name = 'TesT'; 
var nameLowercase = name && name.toLowercase (), 
namesToTest = [ { name : 'asd', otherProperties... },
                { name : 'asd1', otherProperties... },
                { name : 'TesT', otherProperties... }];

var findByCorrectName = R.pipe(
    R.map(
        R.pipe(R.prop('name'), R.toLower)),
        R.find(R.propEq('name', nameLowercase))
    );

var foundObject = findByCorrectName (namesToTest);

return foundObject;
+4
source share
2 answers

I would find this simple way to solve your problem:

const items = [{name : 'asd', x: 1}, {name : 'asd1', x: 2}, {name : 'TesT', x: 3}];

const byNameCi = curry((name, items) => find(
  where({name: pipe(toLower, equals(toLower(name)))}),
  items,
));

byNameCi('TEST', items); //=> {name: "TesT", x: 3}

curry may not be needed for your use case, but it will also allow:

const findAsd = byNameCi('Asd');
// later
findAsd(items); //=> {name: "asd", x: 1}

Note that this returns the original values ​​without converting toLowerCase. If you want to return the names with the converted names, I can change them as follows:

const byTransformedNameCi = curry((name, items) => find(
  where({name: pipe(toLower, equals(toLower(name)))}),
  map(evolve({name: toLower}), items),
));

byTransformedNameCi('TEST', items); //=> {name: "test", x: 3}

, , lensProp over evolve.

, , , , - .

Ramda REPL.


. Ramda ; , Ramda . , -, , , assoc, adjust .

+3

Source: https://habr.com/ru/post/1678735/


All Articles