-2

Edit: total simplification.

Consider this code:

let arr = [42,69]
arr.key = 'lol'

It creates a key value to an array.

However, is it possible to write those two lines in one? I can't find a syntax that works and I'm not sure it is possible.

The reason I want this is because I push the array into another array. I could use an object, but I plan to pop items from it later and it doesn't work on objects.

let all = []
all.push({'0':42,'1':69,'key':'lol'})
14
  • 1
    "it doesn't work" - it's helpful to expand on this. It doesn't appear to be valid syntax, you're mixing up arrays and objects, so presumably you mean it throws an error. But more broadly we've no idea what your inputs are or what output you're expecting. Give a minimal reproducible example. Commented Dec 21, 2021 at 23:57
  • You can't add keys to arrays. You can make an object {key : val} or store the key in the item in the array - again with an object myarray = [{name: "item", key: 500},{name:"item 2", key:501}] Commented Dec 21, 2021 at 23:58
  • Apparently it is called associative arrays. And I can add keys to it if I do it on the next line. pickedrocks[0].key = 500 works perfectly fine. Commented Dec 22, 2021 at 0:00
  • 1
    @agiopnl Why would you need to do that? Commented Dec 22, 2021 at 0:01
  • @Unmitigated, well initially I had only the Rock-objects. I needed to send temporary information with them, so I didn't want to add it inside the class. Commented Dec 22, 2021 at 0:10

1 Answer 1

0

In js you can't have keys in arrays. They only support the index. If you want to assign keys you have to use objects or use the index to do so.

For example like so

const pickedRocks = []

// with objects
pickedRocks.push({value: lastBin.rocks.pop(),key: 500})

// with array index: 0 = value, 1 = key
pickedRocks.push([value: lastBin.rocks.pop(),500])

However since you have a key you could use an object instead of an array alltogether. Unless you want to be able to have duplicates.

const pickedRocks = {}

pickedRocks[500] = lastBin.rocks.pop();
Sign up to request clarification or add additional context in comments.

8 Comments

I didn't think I could either, but pickedrocks[0].key = 500 works perfectly fine. In the console it looks like this: pickedRocks[0] [Rock, fromX: 894.0303571428572, fromY: 966.6660714285714, toX: 1483.5446428571427, toY: 730.9696428571428] 0: Rock {x: 112.46964285714286, y: 87.46964285714286, color: 'hsl(35,540%,60%)'} fromX: 894.0303571428572 fromY: 966.6660714285714 toX: 1483.5446428571427 toY: 730.9696428571428 length: 1 [[Prototype]]: Array(0)
An array is just an object, you can add other arbitrary properties to it. But the array literal syntax doesn't support arbitrary properties. You could do it with e.g. Object.assign, but it's unclear from what you've posted whether that makes sense at all in your context.
@jonrsharpe that is my understanding as well. There are no asociative arrays in js. Here we use Objects or Sets
@VincentMenzel Btw: A couple of guys in this thread says that JS has associative arrays. stackoverflow.com/questions/14313444/…
@agiopnl They are talking about objects. Notice they are defined like so { ... } while an array is defined like this [ ... ]. The objects ALWAYS requires key - value pairs { someKey: 'someValue' }. Values can also be set later using the variable myObject['someKey'] = 'someOtherValue' or using dot notation myObject.someKey = 'someOtherValue'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.