L
L
lunaxoddd2020-08-10 09:43:47
Node.js
lunaxoddd, 2020-08-10 09:43:47

How to use readline in JavaScript to create an array of objects and perform key-value lookups?

It is necessary that the user can first enter the name and age through the console, then it should be possible to get the name when entering the age. Something like this, but I can’t figure out how to enter several object parameters in one line. Or is there some other way to implement this functionality?

const readline = require('readline');
const reader = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let a = [
    {name:"alex", age:'23'},
     { name:"leha", age:'20'},
    {name:"vladislav", age: '21'}
];

console.log('give me age')

reader.on('line', function (line) {
    const input = line
    const find = a.find(a => a.age === input)

    console.log(find)
    
        
    }
)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question