Getting corresponding eNum key for the value passed - javascript

var StateValue = {
Unknown: 0,
AL: 1,
AK: 2,
AZ: 3,
AR: 4,
CA: 5,
CO: 6,
CT: 7,
DE: 8,
},
Now i need to get the enumValues.
function getKeyValue(stateVal) {
For example 'AK'
I need to get the corresponding value...
}

To answer the question in the title (in case someone comes for that), and not the one in the description, you can get the key by the value like this:
Object.keys(StateValue).find(
key => StateValue[key] === 2
)
this will return AK

It is simply:
var val = StateValue[stateVal];
You can access object properties with bracket notation.
I suggest to read MDC - Working with Objects.

var val = StateValue.AK would return 2, just like a regular ENUM

Related

How to write function that filters a dictionary when given a key/value pair in Javascript?

I have a dictionary called teamData
var teamData = {
app: {
sortCol:"name",
sortDir:"asc"
},
data: [
{
id: 1,
name:"Raptors",
coachId: 1,
coachFirst: "Ken",
coachLast: "jenson",
coachPhone: "801-333-4444",
coachEmail: "ken.jenson#uvu.edu",
coachLicenseLevel: 1,
league: 1,
division: 1
},
{
id: 2,
name:"Killer Bunnies",
coachId: 2,
coachFirst: "Peter",
coachLast: "Rabbit",
coachPhone: "801-333-4444",
coachEmail: "peter.rabbit#uvu.edu",
coachLicenseLevel: 1,
league: 1,
division: 2
},
{
id: 3,
name:"Thunderbirds",
coachId: 3,
coachFirst: "Harry",
coachLast: "DirtyDog",
coachPhone: "801-333-4444",
coachEmail: "harry.dirty.dog#uvu.edu",
coachLicenseLevel: 2,
league: 1,
division: 2
}
]
}
I'm trying to write a function that takes a key/value object and returns a filtered dictionary. So if the function is
let teams = filter({coachLicenseLevel:1});
then the expected result is to return a filtered dictionary with only two elements that have that key/value pair
Here is the function I have so far, but I'm stuck on how to get the key object.
filter(filterObj) {
const v = Object.values(filterObj);
const k = Object.keys(filterObj);
const res = teamData.filter(({???}) => v.includes(???));
}
any help would be appreciated.
If you want to filter only the data array, you could do something like this:
function filterArrayByParamAndValue(arr, itemParam, value) {
return arr.filter(item => item.itemParam === value)
}
And in your code just replace the data property, if
let teamData = {
....,
data: [...filterArrayByParamAndValue(teamData.data, coachLicenseLevel, 1)],
....
}
Of course you should also add all necessary checks in the filter function, or even add an object property to check for and pass the whole object.
Instead of passing an object, you may consider using the filter function with your custom filter logic. Here is an example for your specific case:
let teams = teamData.data.filter(item => item.coachLicenseLevel == 1)

Why are the returned data the same?

let statList = {
title: {
x: "center"
},
xAxis: {
type: "category",
axisTick: {
alignWithLabel: true
}
},
yAxis: {
type: "value"
}
};
let statObj = {};
statObj.chatObj = Object.create(statList);
statObj.carObj = Object.create(statList);
statObj.saObj = Object.create(statList);
statObj.chatObj.xAxis.data = [1, 2, 3];
statObj.carObj.xAxis.data = [4, 5, 6];
statObj.saObj.xAxis.data = [7, 8, 9];
console.log(statObj)
Why are the returned statObj.XX.xAxis.data the same?
Why when I use console.log(JSON.stringify(statObj)), the result is {"chatObj":{},"carObj":{},"saObj":{}} ?
While setting statObj.chatObj.xAxis, xAxis is not found on chatObj, so xAxis is searched on the prototype chain (statList is the prototype of chatObj), where we could find it there. Until now we are done with the part statObj.chatObj.xAxis, next we move to create a .data key which will be create on statList.xAxis. Assignments with both statObj.carObj.xAxis.data and statObj.saObj.xAxis.data oberride what was set by statObj.chatObj.xAxis.data on statList.xAxis.data, that is why we have the result of statObj.XX.xAxis.data the last set value which is [7, 8, 9].
Usually I do solve this by using the following trick
statObj.chatObj = JSON.parse(JSON.stringify(statList));
I do not say this is a good practice, but in this way I am making sure the created object has it's own adress in the memory.
The problem with Object.assign() is that it is not working with nested properties, anyway the created object's are mutable.

How can show a someone data in JSON on javascript?

var example = {
"Equation" : [{
"name" : "My Equation",
"xMin" : [41901.0840,4,3,5,2],
"yMin" : [0.0000,4,5,7,8,9,3,1]
}
]
};
var e = JSON.parse(example);
alert(e.example.xMin);
/* What's the problem, can't show any data on array object, at least xMin is Array, how can show someone number on array. Thanks!. */
There is no need to parse since it's already a valid object. To get the property you need to get the Equation property and then it's the first element afterward get xmin property.
var example = {
"Equation": [{
"name": "My Equation",
"xMin": [41901.0840, 4, 3, 5, 2],
"yMin": [0.0000, 4, 5, 7, 8, 9, 3, 1]
}]
};
alert(example.Equation[0].xMin);

Are there limitations for nesting in Javascript? ("Error 21: Undefined is not an object" in Illustrator)

I'm using javascript with Adobe Illustrator CC 2015, and I'm trying to organize information about the artboards.
var myArt = {
types : {
type: "",
board : {
name : "",
refNum : 0,
chk : {}
}
}
};
//initialize
myArt.types = [0, 1, 2, 3, 4, 5, 6, 7, 8];
for (i=0; i<myArt.types.length; i++) {
myArt.types[i].board = [0, 1, 2, 3, 4, 5, 6, 7, 8];
for (j=0; j<myArt.types[0].board.length; j++) {
myArt.types[i].board[j].name = "";
}
};
I get Error 21: Undefined is not an object for the 2nd for loop.
As far as I can tell, what works for "types" should work for "board." The only difference I can see is that board is nested one level deeper. So I guess I'm wondering if there's some kind of limitation on nesting, or if there is some other problem that I'm not catching.
Your overriding myArt.types with an array of numbers. So in your second iteration when you are trying to do myArt.types[0].board.length you are actually calling 0.board.length,1.board.length, but 0.board is undefined. It looks like you are mixing up objects and arrays in javascript. Objects do not by default have a length property.
This is the data structure your loop is implying:
var myArt = {
types: [{
type: "",
board: [{name: "",refNum: 0,chk: {}}]
}]
};

Javascript array indexed by string

I have the below javascript function I want to optimise for my web app.
function DisplayToolTip(str) {
switch (str) {
case "a":
this.tooltip(xvalue,yvalue,text);
break;
case "b":
this.tooltip(xvalue,yvalue,text);
break;
case "c":
this.tooltip(xvalue,yvalue,text);
break;
default: break;
}
}
The switch statement may change i.e. json may need to add in a case "d" but the function exists so dont know how to update the above.
Normally in c# I would use a dictionary, so key would be "a" and value would be an object with properties xvalue,yvalue,text or value would be a string "this.tooltip(xvalue,yvalue,text);".
This way I could update the dictionary and the execution speed of 'DisplayToolTip' would be relatively the same no matter how many elements.
How do you create an array of objects indexed or quickly found using a string value in javascript?
Objects in javascript are like dictionaries.
var dictionary = {
a : ["xvalue","yvalue","text1"],
b : ["xvalue","yvalue","text2"]
}
console.log(dictionary["b"][2]); // will give you text2.
Demo
EDIT: Updated answer to contain arrays (as that is what the question is).
You can use the switch statement itself, with fall-through:
switch (str) {
case "a": case "b": case "c":
this.tooltip(xvalue,yvalue,text);
break;
default: break;
}
(But, as Qantas commented, the default case isn't necessary here.)
Or, it the browser supports it, you can use the indexOf method of arrays:
if (["a", "b", "c"].indexOf(str)) ...
I would do something like this:
var tooltipSettings={
a: {xvalue: 1, yvalue: 1, text: 'string a'},
b: {xvalue: 2, yvalue: 2, text: 'string b'},
c: {xvalue: 3, yvalue: 3, text: 'string c'}
};
function DisplayToolTip(str) {
if(tooltipSettings[str])
{
var settings=tooltipSettings[str];
this.tooltip(settings.xvalue, settings.yvalue, settings.text);
}
}
You could use a dictionary, witch is basically an plain object.
Javascript allows you to access an object property by string like you would access an array property like this:
var obj = {
test: 'text',
b: 4
}
console.log(obj['test'], obj.test);
console.log(obj['b'], obj.b);
So your code would look like this:
var pairs = {
'a': {
xvalue: 1,
yvalue: 1,
text: '1111'
},
'b': {
xvalue: 2,
yvalue: 2,
text: '2222'
}
};
function DisplayToolTip(str) {
var prop = pairs[str];
if (typeof prop !== 'undefined')
return this.tooltip(prop.xvalue, prop.yvalue, prop.text);
throw 'undefined prop ' + str;
}

Categories

Resources