0

Hello guys I'm trying to get each element of an array as the property values of some objects. Therefore I was using a function but it's not dynamic at all because I need to always specify the index of the array after calling the function.

This is my code:

function grab(){ 
 var arr= [123456,987654]
	return arr
}
var arrayObjects= [
 {title: "miami", hash: grab()[0]},
 {title: "new york", hash: grab()[1]}
]
console.log(arrayObjects[0].hash)

What's another more efficient approach?

Cerbrus
73.3k19 gold badges138 silver badges151 bronze badges
asked May 15, 2017 at 11:26
1
  • Why are you using grab as a function? Why don't you just declare the array where you need it? Commented May 15, 2017 at 11:34

3 Answers 3

2

Try like this.Directly Pass the value with in a function

function grab(n) {
return [123456, 987654][n];
}
var arrayObjects = [{ title: "miami" }, { title: "new york"}]
arrayObjects.forEach((a,b)=> a['hash'] = grab(b));
console.log(arrayObjects[0].hash)

answered May 15, 2017 at 11:47
Sign up to request clarification or add additional context in comments.

1 Comment

This is a nice suggestion, the function could have any kind of validation and return null in case of a error or something.
1

Maybe this ?

function grab() {
 var arr = [123456, 987654];
 return arr;
}
var hashes = grab();
var arrayObjects = [{title: "miami"}, {title: "new york" }];
arrayObjects.forEach((item, index) => item.hash = hashes[index]);
console.log(arrayObjects[0].hash);

This is a simple snippet. An index validation could be helpful to avoid out of range exceptions when trying to access hashes array inside the forEach loop. But is just an example.

Cerbrus
73.3k19 gold badges138 silver badges151 bronze badges
answered May 15, 2017 at 11:29

2 Comments

Suggestion: function grab() { return [123456, 987654]; }. Or ditch the function altogether.
@Cerbrus I just copied OP's code because it could be just an example of a bigger function or from another source, idk.
0

If you are worried about indexes, you may wish to turn arr variable into an object with some sort of id specified, and call your hases using this id. For example:

function grab(){
 var hashes = {
 'miami': 123456,
 'ny': 987654
 };
 return hashes;
}
var arrayObjects= [{title: "miami", hash: grab().miami}, {title: "new york", hash: grab().ny}]
answered May 15, 2017 at 11:31

2 Comments

What's the point in using a function to declare an object if you're going to call that function that often. Then at least pass the key into the function and only return the value you need.
Don't ask me, it's asker's snippet :) I just changed it for the sake of an answer, without going too much into clean code, because he never ask for this.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.