Get name of the first child in a object [duplicate] - javascript

This question already has answers here:
How to access the first property of a Javascript object?
(23 answers)
Closed 5 years ago.
I have following Object:
bots: {
bot_1: {
[...]
},
bot_2: {
[...]
},
bot_3: {
[...]
}
...
},
How could I get the name of the first child? In this case 'bot_1'. Thanks!
EDIT: I should have searched a bit more since it is a pretty basic question. I tried searching for the answer but I couldn't find it because I didn't know that it is called "property". Anyways, thanks for your help

You can do this
bots[Object.keys(bots)[0]];
to get the first key of your object. However, you may not get bot_1 always since the keys are unordered

Related

How to use filter to compare to arrays to find missing object? [duplicate]

This question already has answers here:
Array.includes() to find object in array [duplicate]
(8 answers)
Object comparison in JavaScript [duplicate]
(10 answers)
How to determine equality for two JavaScript objects?
(82 answers)
Closed 9 months ago.
I'm trying to use arr.filter() to find the missing element between two arrays, this has been answered plenty of times and i've seen threads like this one Finding missing element in two array for javascript that explain it actually very well. For some reason thought, i cant seem to get this to work.
This is my code
var x = [{Number:1},{Number:2},{Number:3}]
var y = [{Number:1},{Number:2}]
function getDifference(x,y){
x.filter(function(object,index,arr){
console.log(object,index,arr)
if(!y.includes(object)){
// print object
console.log(object) // <-- Prints all 3, should just print the missing one.
}
})
}
getDifference(x,y)
Basically, it just needs to print out the missing object. Which in this case, is {Number:3}
Instead, it prints every object.
I've tried with the code in the thread that i linked above, and still was having trouble. Not sure what i'm not understanding with it.

Undefined as value from Json with Javascript [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 1 year ago.
{
pet: {
"0.628": 92694.5,
"8739.836": 96391.94
},
try: {
//same
}
}
When I specify the key, I get the values but i am trying to read all the values without knowing the keys. I have even tried regex, but nothing seems to be working. As you can see i am fairly new. So sorry if this was a stupid question.
console.log(data.pet) // Gives [Object Object]
console.log(data.pet["0.628"])//Gives the value
console.log(data.pet[0])//Gives undefined
I don't see in what context you'd want to access a json object without knowing the keys.
but what you can do is to parse the json file into a javascript object, and call Object.keys() to get the keys of that object

How to check if the target variable is in an array? [duplicate]

This question already has answers here:
How do I check if a variable is an array in JavaScript?
(24 answers)
Closed 5 years ago.
I'm working on a chatbot in Javascript, and I have a variable called "target", which is the variable that will contain what the user says in chat, and an array called "doingGood":
let doingGood = ["good", "great", "amazing", "awesome"];
What I want to do, is I want to make an if statement that will do something if the target variable is equal to a string that's in the doingGood array. For example, if target is equal to good, great, amazing, or awesome, do something. I've looked around on the internet, but I'm stumped and can't seem to find an answer. Help would be appreciated, thanks!
EDIT: The question is the same, but I tried the methods that they suggested there and they didn't seem to work. When I tried if (variable.target === doingGood), it gave me an error saying "variable is not defined."
you can use indexOf methd of array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
if(doingGood.indexOf(target) !== -1){
// do something
}

How to get the self-DOM object in callback [duplicate]

This question already has answers here:
jQuery Button Click 'this'
(3 answers)
Closed 5 years ago.
$("#subPanel").click(function() {
$("#subPanel").removeClass('btn-success');// it works
this.addClass('btn-default'); it didn't works.
I would like to get self-dom object(in this case $("#subPanel") itself) from inside the call back.
It might be easy problem, so I try to googled around.
However I couldn't get straight answer.
could you help me ??
Inspect this and you will see it's not a jquery object but a DOM element which does not have an addClass method. Try:
$("#subPanel").click(function() {
$("#subPanel").removeClass('btn-success');// it works
$(this).addClass('btn-default')
})
Example: https://jsfiddle.net/14s0h3dr/

How can I dynamically render a variable name in dot notation? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I am not sure how to accurately describe this issue, but the code should suffice. This code is in React.js, but I think it is a JavaScript question more than anything. I have a variable initialization:
const key = this.state.key;
This will evaluate to one of three strings...title, author, or year.
How can I use this variable in the following block?
let filteredBooks = books.filter(
(book) => {
return book.key.toString().toLowerCase().indexOf(this.state.query) !== -1;
}
);
Book has properties title, author, and year... that I want to query, and I would rather not switch through the possible values of key(only 3 right now), in case I want to scale with more properties for the book object later. How can I dynamically reference key, and render it on the fly to either title, author, year, or any other property that key might be. (key is not a property of book). I have tried book.eval(key), but this throws an "undefined is not a function" compilation error at eval(key). Once again, I am using React.js but I am thinking this is a general Javascript question. Thanks!
book[key] is the correct answer posted by Answer Li and Keith in the comments!

Categories