Get single object from array using JavaScript functions [duplicate] - javascript

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 6 years ago.
I have this array of objects:
var frequencies = [{id:124,name:'qqq'},
{id:589,name:'www'},
{id:45,name:'eee'},
{id:567,name:'rrr'}];
I need to get an object from the array above by the id value.
For example I need to get the object with id = 45.
I tried this:
var t = frequencies.map(function (obj) { return obj.id==45; });
But I didn't get the desired object.
How can I implement it using JavaScript prototype functions?

If your id's are unique you can use find()
var frequencies = [{"id":124,"name":"qqq"},{"id":589,"name":"www"},{"id":45,"name":"eee"},{"id":567,"name":"rrr"}];
var result = frequencies.find(function(e) {
return e.id == 45;
});
console.log(result)

You need filter() not map()
var t = frequencies.filter(function (obj) {
return obj.id==45;
})[0];

Related

Using an object clone method and a for loop to push objects into array, the objects are all still the same reference and are the same in the array [duplicate]

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 2 years ago.
let promiseArr = [];
for (let i = 0; i < ISOarr.length; i++) {
options.body.start_time = ISOarr[i];
let newOptions = cloneOptions(options);
promiseArr.push(newOptions);
}
console.log(promiseArr);
Returns an array of the same object.
Clone method:
cloneOptions = options => {
let clone = {};
for( let key in options ) {
clone[key] = options[key]
}
return clone;
}
So my question is how do I push an object that is not the same reference as the previous objects pushed, because even with it supposedly creating a new clone each loop it still somehow creates the same referenced object.
In the loop if I console.log I get the proper output with the value of the key changed, but once it's pushed into the array and we console.log the array, all objects are the same. Any suggestions would be super helpful. Thank you in advance!
Can you try this
cloneOptions = options => {
return JSON.parse(JSON.stringify(options))
}

Assign javascript array to list of JSON objects with a property name? [duplicate]

This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
Closed 3 years ago.
I would like to convert a javascript array which looks like:
['https://www.google.com', 'https://www.facebook.com']
to a list of JSON objects that looks like this:
[{"redirectUri": "https://www.google.com"},
{"redirectUri": "https://www.facebook.com"}]
I have tried using Object.assign({}, array);
however this retuns json with the parameter name as the index of the array value and are all in a single object:
{"0": "https://www.google.com", "1": "https://www.facebook.com"},
is there a way to change this to use a custom parameter name dynamically?
You just need to map your elements respectively, using Array.map() method:
let result = arr.map(o => {
return {
"redirectUri": o
}
});
Demo:
let arr = ['https://www.google.com', 'https://www.facebook.com'];
let result = arr.map(o => {
return {
"redirectUri": o
}
});
console.log(result);

How to select a field inside an array inside an object in Javascript [duplicate]

This question already has answers here:
How to get a key in a JavaScript object by its value?
(31 answers)
Closed 3 years ago.
I have array of object like this :
I need to select only the array which has coreSystemID = 'T24' so I tried credsArr.find() it shows my .find is not a function.
below is my code:
var creds = credsArr.find(x => x.coreSystemID === 'T24')
And I should use the index value like this - credsArr[3053], because the index may vary so I need to select the array which has coreSystemID = 'T24'
let keys = Oject.keys(credsArr).filter(key => credsArr[key].coreSystemID==='T24')
keys will have all the keys of credsArr having credsArr[key].coreSystemID as 'T24'
let systemT24 = credsArr[keys[0]] will give you the object you are looking for given that there is only one object with coreSystemID as 'T24'.
If you have multiple objects that might have coreSystemID as 'T24' you can map it to a new array like this:
let systemT24 = keys.map(key => credsArr[key])
You can use Object.values(credsArr):
var creds = Object.values(credsArr).find(x => x.coreSystemID === 'T24');
or if you need key of the object:
var creds = Object.entries(credsArr).find(x => x[1].coreSystemID === 'T24');
let key = creds[0];
let value = creds[1];

Javascript array of objects check for key [duplicate]

This question already has answers here:
How do I find an array item with TypeScript? (a modern, easier way)
(5 answers)
Closed 5 years ago.
So my question how can I check this array of objects by "key" for eg I want to check if the key 3892 is there, I have tried with indexOf but no luck, I can't use a loop.
You can use some() and hasOwnProperty()
var array = [{3892: 'value'}, {1234: 'value'}];
var check = array.some(obj => obj.hasOwnProperty(3892));
console.log(check)
You can chain Object.keys with Array.prototype.includes to achieve that
Object.keys(myObject).includes(myKey);
const myObject = { name: 'Peter' };
const myKey = 'name';
const result = Object.keys(myObject).includes(myKey);
console.log(`Includes key ${myKey}? `, result);

Convert object key-value pairs to a series of arrays in Javascript [duplicate]

This question already has answers here:
How to convert an Object {} to an Array [] of key-value pairs in JavaScript
(21 answers)
Closed 2 years ago.
I am new to Javascript. I have a Javascript object like so:
s = {"Toothless":"Dragon","Foo":"Bar"};
I need to convert it into a series of arrays, like so:
out = [["Toothless","Dragon"],["Foo","Bar"]];
This is the reverse of what is discussed in Convert JavaScript array of 2 element arrays into object key value pairs. A JQuery solution is acceptable.
You can map over the items to achieve this:
s = {"Toothless":"Dragon","Foo":"Bar"};
var out = Object.keys(s).map(function(data){
return [data,s[data]];
});
console.log(out);
let s = {"Toothless":"Dragon","Foo":"Bar"};
let out = Object.entries(s);
and you get out as an array of small arrays,
see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
var s = {"Toothless":"Dragon","Foo":"Bar"};
var out = [];
for (var key in s){
out.push([key, s[key]]);
}
Try this using jQuery.
var tempArr = [];
s = {"Toothless":"Dragon","Foo":"Bar"};
$.each(s,function(i,v){
tempArr.push([i,v]);
});

Categories