2

I am a beginner programming student and I have some doubts about how to focus and understand this exercise.

Anyone could explain me the logical to face this exercise?

I have an array like this: ['Queen', 'Elizabeth', 'Of Hearts', 'Beyonce'] and I want this result: { Queen : 'Beyonce' }

I want to make function to return the following:

  1. the first element of the array as the object's key
  2. the last element of the array as that key's value. I am doing a var key in array and then a loop through the array

function transformFirstAndLast(array) {
  for (var key in array) {
    for (var i = 0; i < array.length; i++) {
      Object.key[0]
    }
  }
}
Jems
  • 1,666
  • 4
  • 20
  • 50
Asanchez-2
  • 75
  • 1
  • 7
  • 1
    Does this answer your question? [Convert Array to Object](https://stackoverflow.com/questions/4215737/convert-array-to-object) – errorau May 08 '20 at 10:15
  • 1
    You only care about the first and last elements of the array? If that's the case, there's no point in iterating over the whole array. You could just do `let obj = {}` and then `obj[array[0]] = array[array.length-1]`. – lurker May 08 '20 at 10:20

3 Answers3

3

let data = ['Queen', 'Elizabeth', 'Of Hearts', 'Beyonce'] ;

let myobj={}
myobj[data.shift()] = data.pop();

console.log(myobj);
Vahid Alimohamadi
  • 4,900
  • 2
  • 22
  • 37
  • 1
    This mutates the array, which might not be what the OP wants or expects. – Robin Zigmond May 08 '20 at 10:22
  • 1
    As a minor note, the above solution fails for single-element arrays. – BambooleanLogic May 08 '20 at 10:22
  • @RobinZigmond yep, may or may not. – Vahid Alimohamadi May 08 '20 at 10:23
  • @Smallhacker ```{ key : undefined }``` is a failure?! – Vahid Alimohamadi May 08 '20 at 10:25
  • Thanks four your help @VahidAlimohamadi , I don´t understand very well how works shift and pop here in your proposal despite it works in code. Shift delete the first value of the array and pop the last one, but how then it establishes 'Queen' as a object´s key and 'Beyonce' as it values if you are deleting them? Sorry for the question if you considers it obvius ;) – Asanchez-2 May 08 '20 at 10:54
  • @VahidAlimohamadi OP specified that the first value should become the key, the last value should become the value. A strict interpretation of this suggests that the array `['A']` should become the object `{'A': 'A'}`. This is however a relatively unimportant edge case, so I digress. – BambooleanLogic May 08 '20 at 11:19
  • @Asanchez-2 consider the array as a ```stack```. ```obj[key]=value``` eq ```obj.dynamickey = value``` which is a impossible syntax. (JS can behave with Objects as Array too.) – Vahid Alimohamadi May 08 '20 at 16:57
  • @Smallhacker I didn't see that in question but it is so easy to make a conditional check. – Vahid Alimohamadi May 08 '20 at 17:00
0

Hey the way to do this would be using a very simple using a bit of code

function assignObj() {
    object.key = myArray[0];
    object.value = myArray[myArray.length-1];
}

There is no need for a forloop. Unless you need to do that then it is more complicated . Anyways hope this helps. :)

James
  • 137
  • 1
  • 10
0

Arrays are not intended to be used like this. I would highly discourage using this in real life. But this is how you can do it.

  • create an object
  • set the first item of the array as key
  • set the last item of the array as a value
  • return the new object

const array = ['Queen', 'Elizabeth', 'Of Hearts', 'Beyonce'];
console.log( compose_object(array) );
 
function compose_object(  array ) {
  let result = new Object();
  result[array[0]] = array[array.length-1];
  return result;
}
Rob Monhemius
  • 4,822
  • 2
  • 17
  • 49