get the object's attribute value in javascript [duplicate] - javascript

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
I am going to create an array called idArray which contains Id of students in a csv file.
inputData is an array containing 10 objects(records of csv). Each object is an array and have a "Unique Id". I would like to check if I already have this "Unique Id" in my idArray, if no, add it to the array and if yes go to the next record.
getIds: function (inputData) {
inputData.forEach(function () {
if(idArray.length!=0) {
idArray.forEach(function () {
if (inputData."Unique Id"!= idArray)
idArray.push(inputData."Unique Id");
});
}
else
idArray.push(inputData."Unique Id");
});
return idArray;
}
I know that inputData."Unique Id" is wrong, but how can I get Unique Id?
Thanks

Try this
if (inputData["Unique Id"] != idArray)
If the attribute name doesn't have any spaces or special characters in it you can do
object.attributeName
Otherwise you need to reference it inside square brackets
object["attribute Name"]

Related

Conitnuous looping for while loop [duplicate]

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
How to find if an array contains a specific string in JavaScript/jQuery?
(7 answers)
Closed 7 months ago.
while (true) {
if (memberName == subMemberGroup.indexOf("Leonardo", "Catherine", "Luther", "Bruce", "Amy") !== -1) {
console.log("\nMember's name exists in database. Please enter a new name.");
var memberName = input.question("Please enter member's name: ");
} else break;
}
I am trying to loop through to see if "memberName" that the user has input matches any of the following strings in the "subMemberGroup" array.
However, when I tried using while loop, it keeps saying that Member's name exist even thought the "memberName" that the user input does not match the strings in "subMemberGroup" array. I tried using if statement with a break but it still does not work.
The reason why I used while loop is because I want my code to re-prompt the user the enter a new name if the "memberName" matches the strings in "subMemberGroup" array.

How to update a javascript/json object based on a string defining path of field to be inserted to object [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 3 years ago.
My object is already defined.
I want to insert variables at particular indexes in the object.
Given message object:
message=[{
person:{
actions:[{test:1},{test:2}]
},
person:{}
},
{
person:{
actions:[{test:3},{test:4}]
},
person:{}
}]
1) If my string = "message[0].person.actions[1].visible".
This means I need to insert visible=null at index=1 of actions.
So message object becomes:
message=[{
person:{
actions:[{test:1},{test:2,visible:null}]
}
},
{
person:{
actions:[{test:3},{test:4}]
}
}]
2) If string="message[1].person.accessible",
So message Object become:
message=[{
person:{
actions:[{test:1},{test:2}]
}
},
{
person:{
accessible:null,
actions:[{test:3},{test:4}]
}
}]
I thought of doing split('[') and get that index and insert in that object.
Let me know if there are any libraries out there simplifying this for more complex data structure.
This question got closed along with a link to question [Accessing nested JavaScript objects with string key
My question is different than above mentioned link. I am asking about inserting a new field at particular location in object.
Just mutate the object by assigning it a property.
message[0].person.actions[1].visible = null;
This should work.

I want toarray in JavaScript? Where is it my fault? [duplicate]

This question already has answers here:
Object property name as number
(6 answers)
Closed 4 years ago.
I add This code to my page and I want to complete city when user tap a zipcode
and my alert dosen't show
var obj = {
"01400": "ABERGEMENT-CLÉMENCIAT",
"01640": "ABERGEMENT-DE-VAREY",
"01500": "AMBÉRIEU-EN-BUGEY",
"01330": "AMBÉRIEUX-EN-DOMBES",
"01300": "AMBLÉON",
"01500": "AMBRONAY",
"01500": "AMBUTRIX",
"01300": "ANDERT-ET-CONDON",
"01350": "ANGLEFORT",
"01100": "APREMONT",
"01110": "ARANC",
"01230": "ARANDAS",
"01100": "ARBENT",
"01300": "ARBIGNIEU",
"01190": "ARBIGNY"
};
var myVariable = obj .01400;
alert(myVariable);
Firstly, there is no key named 97433 in your object
Secondly, even if there was you cannot use property accesors with object keys which begin with a number. You need to use bracket notation.
Lastly, use console.log() for debugging as alert() coerces types and blocks the UI logic.
var myVariable = obj['97433'];
console.log(myVariable);

How can i use value from input to console.log a value from an object? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 5 years ago.
I have an object that contains all the months:
const months = {
"january": 31,
etc...
Then i have a text input. I select the value from the input with javascript (.value) in a variabel called "value".
When I type "january" (without quote marks) in the text input and try doing console.log(months.value); nothing happens.
How can I convert the value a datatype (or something else) that can be used to select "january" in the object "months"?
A string is fine to select an item from an object, but you should use square bracket notation when you want to get your key name from a variable:
console.log(months[value])
As far as I understand you need this months[“january”]
Or months[value]

How to access Json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I am trying to read from a JSON array with a nested array which has its value name spaced out. So I get an error whenever I run the code.
var error = [
{
"LessonName":"Understanding Multiplication",
"LessonID":"13343",
"no of questions":[{"Locked":"31","Unlocked":5}]
},
{
"LessonName":"Finding Unknown Values ",
"LessonID":"13424",
"no of questions":[{"Locked":"34","Unlocked":5}]
}
]
function jsd(){
document.write(error[0].LessonName);
document.write(error[0].'no of questions'[0].Locked);
}
document.write(error[0]."no of questions"[0].Locked); Doesn't seem to display.
You may use a property accessor with brackets for the string.
error[0]['no of questions'][0].Locked
You must use this syntax for strings with spaces.
document.write(error[0]['no of questions'][0].Locked);

Categories

Resources