Jump to content

Javascript Object

Go to solution Solved by vorticalbox,

Your bestidever in objecttwo is an array so you need to put the index you wish to access.

var objectone = {
	"id":"bestidever"
}
var objecttwo = {
	"bestidever":[{"myValue":"print me!"}]
}
var item = objecttwo[objectone.id]
console.log(item[0].myValue) //displays print me!

or if you want it in one line

console.log(objecttwo[objectone.id][0].myValue)

 

I'm not exactly sure what you're planning to use this for but hope this helped

Hey,

I'm trying to get this working:

var objectone = {
	"id":"bestidever"
}

var objecttwo = {
	"bestidever":[{
		"myValue":"print me!"
	}]
}

console.log(objecttwo[objectone.id].myValue)

but it doesn't work? can someone help me?

@Minibois

@Sintezza

Link to comment
https://linustechtips.com/topic/611059-javascript-object/
Share on other sites

Link to post
Share on other sites

3 minutes ago, ddddanil said:

You missed = in objecttwo

 


var objecttwo = {

"bestidever":[{

....

}]

}

 

ah, didn't see that, but i tried it, and it doesn't solve the problem

have adjusted the original post

Link to comment
https://linustechtips.com/topic/611059-javascript-object/#findComment-7901311
Share on other sites

Link to post
Share on other sites

13 minutes ago, ddddanil said:

well you use your obj2 like an array of objects (name[array].object), use it like name.object[array].

you mean? because that wouldn't work

console.log(myValue.objecttwo[objectone.id])
Link to comment
https://linustechtips.com/topic/611059-javascript-object/#findComment-7901376
Share on other sites

Link to post
Share on other sites

Your program is strange.

In theory this would work:

objecttwo.(objectone.id)["myValue"];

    ^                  ^                     ^

original obj       |                      |

                 obj's field                |

                                        field - array

Link to comment
https://linustechtips.com/topic/611059-javascript-object/#findComment-7901381
Share on other sites

Link to post
Share on other sites

1 minute ago, ddddanil said:

use . to access to obj's field like cat.name

use [] to access array's value like cats[myCat]

combine them as you want like cats[myCat].name or myCat.friends['Kolly']

can write some code that would do what i want?

Link to comment
https://linustechtips.com/topic/611059-javascript-object/#findComment-7901406
Share on other sites

Link to post
Share on other sites

Your bestidever in objecttwo is an array so you need to put the index you wish to access.

var objectone = {
	"id":"bestidever"
}
var objecttwo = {
	"bestidever":[{"myValue":"print me!"}]
}
var item = objecttwo[objectone.id]
console.log(item[0].myValue) //displays print me!

or if you want it in one line

console.log(objecttwo[objectone.id][0].myValue)

 

I'm not exactly sure what you're planning to use this for but hope this helped

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/611059-javascript-object/#findComment-7901438
Share on other sites

Link to post
Share on other sites

1 hour ago, vorticalbox said:

Your bestidever in objecttwo is an array so you need to put the index you wish to access.


var objectone = {
	"id":"bestidever"
}
var objecttwo = {
	"bestidever":[{"myValue":"print me!"}]
}
var item = objecttwo[objectone.id]
console.log(item[0].myValue) //displays print me!

or if you want it in one line


console.log(objecttwo[objectone.id][0].myValue)

 

I'm not exactly sure what you're planning to use this for but hope this helped

thanks :D
i was messing around with that 0 but it didn't quite work, but your solution works :D

Link to comment
https://linustechtips.com/topic/611059-javascript-object/#findComment-7901696
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×