Imagine you have the following array of objects:
var myObject =
[
{A:1, B:2, C:3},
{A:4, B:5, C:6},
{A:7, B:8, C:9},
]
How would you efficiently retrieve a subset with the Y values from this array?
var subset = [ B:2, B:5, B:8 ]
Imagine you have the following array of objects:
var myObject =
[
{A:1, B:2, C:3},
{A:4, B:5, C:6},
{A:7, B:8, C:9},
]
How would you efficiently retrieve a subset with the Y values from this array?
var subset = [ B:2, B:5, B:8 ]
let list = [
{A:1, B:2, C:3},
{A:4, B:5, C:6},
{A:7, B:8, C:9},
];
const result = list.map(({B}) => ({B})); // [{B: 2}, {B: 5}, {B: 8}]
This code snippet utilizes an arrow function to iterate over the array, extracting the B
property from each object, and creating a new object with just the B
property using object property shorthand.
Using an arrow function with <code>map
adds a touch of elegance:
let myArray =
[
{A:1, B:2, C:3},
{A:4, B:5, C:6},
{A:7, B:8, C:9},
];
let result = myArray.map(item => ({B: item.B}));
console.log(result);
This code becomes even more sleek when you incorporate destructuring as demonstrated by Jared. :-)
To accomplish this task, utilize the map function
var data = [
{A:10, B:20, C:30},
{A:40, B:50, C:60},
{A:70, B:80, C:90},
];
var result = data.map(item => {
return {
B: item.B
};
});
console.log(result);
Although I may not have fully grasped the requested output notation, you can still utilize a map function:
myObject.map(item => item.Y) // Produces [2, 5, 8]
myObject.map(item => ({Y: item.Y})) // Generates [{Y: 2}, {Y: 5}, {Y: 8}]
If you're aiming for maximum code reusability, one elegant approach is to incorporate some currying into your functions:
const arr = [
{X:1, Y:2, Z:3},
{X:4, Y:5, Z:6},
{X:7, Y:8, Z:9},
];
const get = prop => o => ({[prop]: o[prop]});
const extract = arr => prop => arr.map(get(prop));
const result = extract(arr)('Y');
console.log(result);
Hi there, I've been attempting to implement Internationalization in my stencil project but unfortunately, it's not working as expected. I'm not sure what's causing the issue, and all I'm seeing is a 404 error. I followed these arti ...
// Sending an HTTP request to fetch JSON data $http({ method: 'GET', url: '/Home/GetJson' }).then(function successCallback(response) { console.log(response); $scope.jsonData = response; var data = response.data ...
Embarking on a new journey with AngularJS and ASP.NET WebAPI, I find myself faced with the challenge of working with base tables structured as follows: CurriculumID SubjectArea CourseNumber ------------ ----------- ------------ 303 GHIJ 1 ...
Here is the structure I'm working with: <div class="checkmark-outer"> <div class="checkmark-33"> <div class="fa-stack fa-1x checkmark-icon"> <i class="fa fa-circle fa-stack-2x icon-background"></i> ...
Is it possible to utilize the es6 Map type in an HTTP Response DTO? Let's consider an Angular 2 request: public loadFoos(): Observable<FoosWrapper> { return this.http.get("/api/foo") .map(res => res.json()); } Now, take a loo ...
// env.development.js module.exports = { fileName: 'logo_dev.png' } // env.production.js module.exports = { fileName: 'logo_prod.png' } // index.vue <script> import logoSrc from '@/assets/images/' + process.env.fil ...
I've been doing some research but I'm struggling to find a good solution for this issue. I have a response that looks like this: "name": "another test", "description": "para hacer el aseo", &quo ...
When my Angular 6 app makes a request to the ASP.NET Core web API using the GET method, I want to send a list or array of unique identifiers as parameters. In return, I expect only information relevant to those identifiers to be retrieved from the API. He ...
Within my form, I have a jhtmlarea textarea element: textarea id="txtDigital" name="txtDigital" class="form-control" style="background-color:white; resize: vertical !important; " rows="20" placeholder="Details" ...
Hello, I'm a Java engineer diving into the world of Javascript for the first time. Apologies in advance if my information is lacking or incorrect! I am currently working on a gulp build script and utilizing bower to manage dependencies for my JS fron ...
Can someone assist me in translating the Java Regex code below to JavaScript Regex? (\\\p{Upper}{2})(\\\d{2})([\\\p{Upper}\\\p{Digit}]{1,30}+) I attempted using the following JavaScript Regex: ...
While developing a javascript-based web widget, I am aiming to steer clear of using iframes and avoid relying on the page's CSS. It is infeasible for me to utilize custom CSS, as it defeats the purpose, and iframe integration would not present a user- ...
Currently, my focus is on a react application where I am retrieving a JSON response from a specific route that consists of a collection of JSON objects. My objective now is to introduce a new field in each JSON object based on another field within the sam ...
Apologies for the unclear title. I am facing a bit of confusion with my current project. I am in the process of developing a web front-end for an academic simulation tool, which includes a C++-based simulator that is efficient for small systems but produce ...
Hey there, I'm trying to save the response from an HTML page and store the content of that page. However, every time I try, I keep getting an error message. What am I doing incorrectly? <script type="text/jscript"> var page = ""; $.aj ...
Utilizing jqGrid proves to be challenging when it comes to extracting attributes from an XML file. In order to overcome this limitation, I am seeking assistance in creating an array from the XML data provided below. The ideal array format for jqGrid is as ...
I've been attempting to track when the browser goes into fullscreen mode. Everywhere I look, this blog is mentioned as the go-to resource on the fullscreen API. According to this SO answer, it should work. Fullscreen API: Which events are fired? B ...
This specific segment of code is the focus right now, Here is where my attention lies const [array, setArray] = useState( JSON.parse(localStorage.getItem("notes")) ?? [] ); And here we have the function in question, const save = () => ...
I am trying to enhance the visibility of the drop-down caret, but currently it is not showing up as desired. My goal is to give both the <select> and <button> elements a circular radius similar to what is shown in the image. .top-select{ ...
Encountering the 'safe is undefined' error while interacting with HTML that has been dynamically inserted into a page via an AJAX call. For example, when selecting an option from a dropdown within this HTML, the error occurs and the dropdown rese ...