Search in JavaScript object with wildcards? [closed] - javascript

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 4 years ago.
Improve this question
I have an array of objects which I would like to contain wildcards.
Is it possible to somehow implement wildcards in my objects' keys and allow this type of matching?
dict = [{"foo*" : "A" }, {"bar*" : "B"}]
if (dict["foo_1_2"]){
console.log("FOUND");
}

one way you can do is iterate over array get key of each object and test with regx
var key="foo_1_2";
var dict = [{"foo*" : "A" }, {"bar*" : "B"}]
function isKeyExists(key,array){
for(var i in array){
var regX = new RegExp('^'+Object.keys(array[i]),"g");
if(regX.test(key)){
return true;
}
}
return false;
}
console.log(isKeyExists(key,dict));

Related

convert a javascript object's value-field to array [closed]

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 1 year ago.
Improve this question
suppose the object is obj ={foo:"bla,bla2,bla3",bar:"bla,bla5"}
I want to convert it to 'obj={foo:["bla","bla2","bla3"],bar:["bla","bla5"]}'
Don't know why you want to do that, but here you can just loop through obj with a for...in loop and split the strings by commas:
const obj = {foo:"bla,bla2,bla3",bar:"bla,bla5"};
for (name in obj) {
obj[name] = obj[name].split(',');
}
console.log(obj)

How to Filter an array of strings based on first word in the string in javascript? [closed]

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 1 year ago.
Improve this question
With the following array of strings in javascript, is there a way to use the .filter to filter by the first word?
['Desk_One', 'Desk_Two', 'Desk_Three', 'Chair_One', 'Chair_Two']
For example to .filter(x = 'Desk_*')
['Desk_One', 'Desk_Two', 'Desk_Three']
Maybe this needs to be a regex
You can use startsWith() function like this:
let array = ['Desk_One', 'Desk_Two', 'Desk_Three', 'Chair_One', 'Chair_Two'];
let result = array.filter((item)=>item.startsWith(array[0].split('_')[0]));
console.log(result)

Comparing a string array with object array [closed]

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 3 years ago.
Improve this question
I want to compare two arrays
array1=["123',"456"];
array2=[{id":"001",name:"prashant"},{id:"123",name:"jhh"},{id:"123444",name"baak"},{id:"456",name"sxs"}];
my objective is to extract the objects from array2 whose ids match the values in array1.
Can someone help me with the optimal solution?
First of all your second array (array2) is syntactically invalid.
You can try with Array.prototype.filter() and Array.prototype.includes()
var array1=["123","456"];
var array2 = [{id:"001",name:"prashant"},{id:"123",name:"jhh"},{id:"123444",name:"baak"},{id:"456",name:"sxs"}];
var res = array2.filter(i => array1.includes(i.id));
console.log(res);

Key-value to object [closed]

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 8 years ago.
Improve this question
I got a key-value object like this:
items = {1:"a",2:"v",3:"u"};
But i want it like this.
items = [{"key":"1","value","a"},{"key":"2","value","v"},{"key":"3","value","u"}];
What is the best way to do this?
I have already tried to do this with $.each.
var items = {1:"a",2:"v",3:"u"};
var newItems = [];
$(items).each(function(k,v){
newItems.push({"key":k,"value":v});
});
And please tell me what is wrong with this question!
Try this-
var items = {1:"a",2:"v",3:"u"};
var obj=[];
for(var i in items){
obj.push({"key":i,"value":items[i]});
}
console.log(obj)

How to properly iterate through nested arrays with javascript. [closed]

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 years ago.
Improve this question
I am having a hard time learning how to iterate through nested arrays, and storing specific array values in javascript.
$('#btn2').click(function(e){
e.preventDefault();
var res = hjs.getValue([1,2,3],['bri', 'alert','name', 'hue', 'sat']);
console.log(res);
});
When #btn2 is clicked on, the following is outputed to my console:
If I'd like to access anyone of these values how would I do so? How do I store the value in a variable?
you can iterate through the object and get the value you want:
$('#btn2').click(function(e){
e.preventDefault();
var res = hjs.getValue([1,2,3],['bri', 'alert','name', 'hue', 'sat']);
for(var i in res)
{
if (res.hasOwnProperty(i)) {
var obj = res[i];
console.log('name: ', obj.name);
console.log('sat: ', obj.sat);
}
}
});

Categories