Aleksandar Ivanov
Aleksandar Ivanov

Reputation: 317

Combine two arrays into one array of objects with two properties

I have two arrays:

arrayNumberNames=['one', 'two', 'three']
arrayDigits=[1,2,3]

I want to combine them into an array that looks like this:

arrayNumbers=[
   {
       name: 'one'
       digit: 1,
   },
   {
       name: 'two'
       digit: 2,
   },
   {
       name: 'three'
       digit: 3,
   }
]

Upvotes: 0

Views: 77

Answers (1)

Ivan
Ivan

Reputation: 7277

In bash you can create associative array like this:

declare -A arrayNumbers

And populate it like this

for i in ${arrayDigits[@]}; { arrayNumbers[${arrayNumberNames[$i]}]=$i; }

Array will look like this

arrayNumbers=( [one]=1 [two]=2 [three]=3 )

Upvotes: 2

Related Questions