score:1
Accepted answer
const countries = [{ id: 2, code: "AL", name: "Albania" }, { id: 3, code: "DZ", name: "Algeria" }, { id: 4, code: "DS", name: "American Samoa" }];
function getCountryId(code) {
const country = countries.filter(country => country.code === code);
if(country.length > 0) {
return country[0].name;
} else {
return "No such country.";
}
}
console.log(getCountryId("DZ"));
console.log(getCountryId("USA"));
score:0
var data = [{ id: 2, code: "AL", name: "Albania" }, { id: 3, code: "DZ", name: "Algeria" }, { id: 4, code: "DS", name: "American Samoa" }]
Array.prototype.getCountryID = function(code){
var output = this.filter(el => el.code === code);
return output.length > 0 ? output[0].id : "Not Found";
}
console.log(data.getCountryID("DS"));
console.log(data.getCountryID("something else"));
score:1
const list = [{
id: 2,
code: "AL",
name: "Albania"
}, {
id: 3,
code: "DZ",
name: "Algeria"
}, {
id: 4,
code: "DS",
name: "American Samoa"
}];
function getCountryId(name) {
return (list.filter((country) => country.name === name)[0] || {}).id;
}
console.log(getCountryId('Algeria'));
console.log(getCountryId('NoneExistingCountry'));
score:3
const country = array.getCountry("USA");
console.log(country && country.id);
Source: stackoverflow.com
Related Query
- how could i return the value inside of a foreach javascript
- How to store the return value of a function inside a variable in reactJS
- setstate object:{array:[]} in reactjs how could i add the **key and value inside the array which is in the state object**?
- How to properly return value from a data inside the function
- How to show the value from an if statement inside a foreach loop in react js?
- How to split a long string into a multi-line string, but the string is a value inside of a javascript object
- How do I fix "Expected to return a value at the end of arrow function" warning?
- How to add comment inside the return statement in render function?
- Redux Toolkit w/ TypeScript: how can I properly type the return value from an async thunk
- How can I export the type of an Action creators return value using Flow?
- How to fix 'Expected the return value to be a 31-bit integer' error react hooks
- How to get value from useState inside the function
- How do i use/print the value inside a generic attribute in TestCafe
- How can I check the return value of dispatch in redux hook
- React : how to pass and array from inside a Function to the return (JSX)
- How can I pass the argument up to the onClick function when it is called by reference inside return tag in reactjs?
- javascript (es6) return value of filter in forEach
- how to get the promise value of return promise
- How do I fix a React TypeScript error with the return value of useHotkeys hook not matching the type of the div element ref prop
- Recharts: how is it possible to show only the value of ONE data point inside a Tooltip?
- How to show a certain value inside the input tag in reactjs?
- How do I pass up the value inside of react-autosuggest to the parent component?
- How to reference a javascript variable to be the value of the props of a component?
- how do i update state in react for updating the value of an object inside an array using array.map function
- return value is not rendering when returned from inside the handleclick() function in react
- how to print the elements value inside an object react using es6
- When selecting an option in a dropdown, how do I change the state with onChange but with a value other than the one inside the option tags
- How to change the value of all the objects inside the expect except the one which is clicked in React js?
- how to displayed key and value in functional component inside return part
- How does JavaScript map() function iterate until the array size while it's having a return inside?
More Query from same tag
- How to setState to one particular component instance in RefluxJS?
- Export default in Reactjs
- React app not appearing on live server (works locally) - No visible errors, suspect backend issue
- Styled component, breaks on google chrome
- How to set database dynamically based on client request in Laravel
- React Mocha rendering, DOMException Wrong Document
- Why does material UI react stepper doesn't have tab focus?
- What's the difference between alignItems and alignSelf in React Native?
- how display node array in react front
- ReactJS with Typescript not accepting Object with properties
- forEach works on FileList on CRA/Next JS app, but not on Next JS app created with create-next-app
- getValue() for SelectField
- How to add a dividier to an javascript object when a value changes?
- How can I avoid a TypeScript error with React useRef?
- Laravel Sanctum & React with Axios, POST return 419
- What is the proper way to use multiple layouts in ReactJS
- Good way to interact between different and same levels of depth (nested) in ReactJS ('States' + Hooks)
- Correct way to get refs and attach event listeners in React ComponentDidMount?
- How to use Root Import with React Testing Library in React?
- Jest - Could not locate module with dynamic import template string
- dynamic import returns {$$typeof:...,render:f...} instead of the exported class
- Nodejs - How to acquire ID property whenever you change the value on a selection option object
- Using lodash and trying to update and Object with multiple objects. (editing items)
- React useReducer not showing value from array.reduce
- Displaying validation errors in Laravel 5 with React.js and Ajax
- Image not getting displayed when using Reactjs map function. Error--SRC undefined
- React loading screen using hooks
- Cannot upload image from React frontend to flask backend
- How to set Material UI checkbox list checked by default?
- pass named and unnamed arguments to stateless component