This question already has answers here:
Filter strings in Array based on content (filter search value)
(6 answers)
Closed 4 years ago.
I have an array of strings (names) such as:
name =["John Doe","Lutfur Kabir", "Moshiur Imtiaz Rahman", "Clark Kent","Jenny Doe"]
I want to get the index/es of the name that has Doe in it. How do I go about doing it using JavaScript.
You can use Array.prototype.includes:
Find the previous answer here already
var categoriesPresent = ['word', 'word', 'specialword', 'word'];
var categoriesNotPresent = ['word', 'word', 'word'];
var foundPresent = categoriesPresent.includes('specialword');
var foundNotPresent = categoriesNotPresent.includes('specialword');
console.log(foundPresent, foundNotPresent); // true false
You can use .reduce() to create an array having indexes of strings containing the desired string:
let data =["John Doe","Lutfur Kabir", "Moshiur Imtiaz Rahman", "Clark Kent","Jenny Doe"];
let result = data.reduce((r, c, i) => {
if(c.includes('Doe')) { r.push(i); }
return r;
}, []);
console.log(result);
Related
This question already has answers here:
How do you update an object in an array based on another array?
(3 answers)
Closed 3 months ago.
I have javascript object like this
var arr1 = [{name:'qqq'},
{name:'www'},
{name:'eee'},
{name:'rrr'}]
var arr2 = [{value:'qqq',url:'123'},
{value:'www',url:'456'}]
I need to replace objects in arr1 with url from arr2 with matching name and value.
So here is the result I want to get:
var arr1 = [{name:'123'},
{name:'456'},
{name:'eee'},
{name:'rrr'}]
So you need to loop through arr1 and filter arr2 by name value, if you will get result, then replace name value with url value:
let replacedValues = arr1.map(o1 => {
let fResult = arr2.filter(o2 => o2.value === o1.name)[0]
if(fResult) {
o1.name = fResult.url
}
return o1
})
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);
This question already has answers here:
Convert an array to an array of objects
(4 answers)
Closed 4 years ago.
I have the following array of string that needs to be turn into array of object with the keys value and label
languages: ["Arabic", "Irish"]
0:"Arabic"
1:"Irish"
length: 2
If I wanted to make this array into an array of object like the following:
languages = [
{ value: 'Arabic', label: 'Arabic' }
{ value: 'Irish', label: 'Irish' }
]
How will I do that? Can someone please help?
You can use Array.map():
var languages = ["Arabic", "Irish"];
var res = languages.map(item => ({'value':item, 'label':item}));
console.log(res);
You can also use Array.forEach() as:
var languages = ["Arabic", "Irish"];
var res = [];
languages.forEach(item => res.push({'value':item, 'label':item}));
console.log(res);
This question already has answers here:
How to transpose a javascript object into a key/value array
(11 answers)
Closed 5 years ago.
I want to convert an object of key values to an array of objects in javascript
var obj={"name1":"value1","name2":"value2",...};
How can i convert it to
arr=[{"name":"name1","value":"value1"},{"name":"name2","value":"value2"},...];
Try with array#map and Array#push
var obj={"name1":"value1","name2":"value2"};
var res=[];
Object.keys(obj).map(a => res.push({name:a , value:obj[a]}))
console.log(res)
Short answer (ES6):
const arr = Object.entries(obj).map(([name, value]) => {
return {
name,
value
}
});
Another answer (ES5):
var arr = Object.keys(obj).map(function(key) {
return {
name: key,
value: obj[key]
}
});
This question already has answers here:
Sort an array based on another array of integers
(7 answers)
Closed 7 years ago.
I have an array or numbers and objects (same length):
var a = [2,0,1], b = [obj1,obj2,obj3];
I would like to reposition items in array 'b' to the position of numbers in array 'a'.
Could be done with jquery as well.
How can I do that most easily?
Thanks
To do this there is no an auto function but you can use array map to get this result.
var orderByArray = function(order, data) {
return order.map(function(pos) {
return data[pos];
});
};
var a = [2,0,1];
var b = ['obj1', 'obj2', 'obj3'];
var result = orderByArray(a, b);
console.log('result', result);