Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have following piece of code:
var search_value = 'XYZ';
var FIELD_MAP = {
'key1': [
'SOME',
'THING'
],
'key2': [
'ANOTHER_VALUE',
'XYZ'
]
};
I need to find which array (key1, key2 etc) has a value stored in variable search_value. What is the best way to do this?
This can be achieved by iterating over the properties of FIELD_MAP and checking if the required search string is available in any of them:
for (var key in FIELD_MAP) {
if (FIELD_MAP.hasOwnProperty(key)) {
if (FIELD_MAP[key].indexOf(search_value) > 0) {
console.log("Found in=",key)
}
}
}
You can do it this way by filtering keys on FIELD_MAP object by checking if the value contains the search_value by using Object.keys to get keys and Array.filter to filter them.
var key = Object.keys(FIELD_MAP).filter(function(k){
return ~FIELD_MAP[k].indexOf(search_value);
})[0]; // remove the [0] if you want multiple keys matching the criteria
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
const arrFilter = [{id:1},[],[]];
how can I filter the empty array in the above example, so that the result would be the object with an id only
You can use the length of the array returned from Object.keys() to determine if objects, arrays or strings are empty however it won't work on numbers.
With that in mind, try this assuming that
all empty objects, arrays and strings should be omitted
everything else stays
const arrFilter = [{id:1},[],[], "a string", "", 1, 0];
const nonEmpties = arrFilter.filter(
(item) => typeof item === "number" || Object.keys(item).length > 0
);
console.log(nonEmpties);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
My array
const array = [2,3];
and I want object like
"media" :[{"mid":"2"},{"mid":"3"}]
Thanks in advance.
You can use Array.prototype.map to create the desired array and then you can create an object and assign the array to the media key.
const array = [2, 3];
const obj = { media: array.map((item) => ({ mid: item })) };
console.log(obj);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a search query like
`const query = '?sortBy=recent&page=2&comments=true&sortBy=rating' // two repeating params 'sortBy'
How can I use regex for checking is there any repeating params ????
Not recommended to use regex.
Try this
const query = new URLSearchParams('?sortBy=recent&page=2&comments=true&sortBy=rating');
const keys = [...query.keys()]; // convert iterable to array
console.log(keys)
const unique = keys.length === new Set(keys).size; // return false if dupes found
console.log(unique);
// to get the dupe(s)
const dupes = keys.filter((e, i, a) => a.indexOf(e) !== i)
console.log(dupes)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to create an array of objects in which each objects has 4 values. All coming from 4 diferent arrays.The arrays are long.I have no idea on how to do it. It's pretty difficult i think, i have been looking for hours vv'.
var local=[F.C Barcelona, Real Madrid, Manchester United.....];
var away=[ Manchester City, PSG, Liverpool....];
var matchDay[2,3,4,5....];
var score=[2-0, 0-1, 2-2...];
// array to hold the objects
let arr = []
// assuming the four arrays are all the same length
// just pick one to use for the length
for(let i = 0; i < local.length; i++) {
// create a new object with the 4 fields, one from each array
// and grab the i'th entry of each array for it
let obj = {
local: local[i],
away: away[i],
matchDay: matchDay[i]
score: score[i]
};
arr.push(obj);
}
Not sure exactly what you're trying to do, but something like this would work.
> array1.map(function(item, index){
return {key1: item,
key2: array2[index],
key3: array3[index]
};
});
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Example: I have an array with repeated values 1 and 2
[1,1,2,2,3,4,5]
I want the result of that array to be an array of the values that dont repeat.
[3,4,5]
var arr = [1,1,2,2,3,4,5]
arr = arr.filter (function (value, index, array) {
return array.indexOf (value) == array.lastIndexOf(value);
});
console.log(arr);
https://jsfiddle.net/qducmzqk/
Without JQuery, you can use the filter method:
var nums = [1,1,2,2,3,4,5]
nums = nums.filter(function(val){
return nums.indexOf(val)===nums.lastIndexOf(val);
});
// [3,4,5]
Otherwise, if in future you want to preserve repeated numbers, you can use:
for(var i=0; i<nums.length; i++) if(i!==nums.lastIndexOf(nums[i])) nums.splice(i, 1);
// [1,2,3,4,5]