Objects with given Fruit
Given an array of objects
objectEntities in the prefilled code and fruit as an input, write a JS program to log the array of objects containing the given fruit.
Input
The input will be a single line containing a string
fruitOutput
The output should be the array of objects matching the given
fruit
Sample Input 1
apple
Sample Output 1
[
{ fruit: 'apple', vegetable: 'broccoli' },
{ fruit: 'apple', vegetable: 'cauliflower' }
]
Sample Input 2
orange
Sample Output 2
[ { fruit: 'orange', vegetable: 'mushrooms' } ]
i want code in between write your code here
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString.trim().split("\n").map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
/* Please do not modify anything above this line */
function main() {
const fruit = readLine();
const objectEntities = [
{
fruit: "apple",
vegetable: "broccoli"
},
{
fruit: "kiwi",
vegetable: "broccoli"
},
{
fruit: "apple",
vegetable: "cauliflower"
},
{
fruit: "orange",
vegetable: "mushrooms"
},
];
/* Write your code here */
}
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString.trim().split("\n").map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
/* Please do not modify anything above this line */
function main() {
const fruit = readLine();
const objectEntities = [
{
fruit: "apple",
vegetable: "broccoli"
},
{
fruit: "kiwi",
vegetable: "broccoli"
},
{
fruit: "apple",
vegetable: "cauliflower"
},
{
fruit: "orange",
vegetable: "mushrooms"
},
];
/* Write your code here */
const res = objectEntities.filter( item => item.fruit == fruit )
console.log(res);
}
Comments
Leave a comment