-1

What is the best way to convert:

["Foo", "Bar", "John"]

To:

{
Foo: { name: 'Foo', index: 0, type: 'String' },
Bar: { name: 'Bar', index: 1, type: 'String' },
John: { name: 'John', index: 2, type: 'String' },
}

I believe I need to utilize

array.map()

but am not sure how to structure my mapping function. Any insight would be helpful.

3 Answers 3

1

You can the function Array.prototype.reduce as follow.

const source = ["Foo", "Bar", "John"],
  capitalize = string => string.charAt(0).toUpperCase() + string.slice(1),
  result = source.reduce((a, name, index) => ({...a, [name]: {name, index, type: capitalize(typeof name)}}), {});
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! super helpful. The result needs to be an object, although that wasn't clear in the question. I have updated the exact structure needed.
@NicoloCode you can use reduce for that. something like given that keynames are distinct stackoverflow.com/questions/64188099/…
@NicoloCode you cannot have multiple repeated keys within an object.
@NicoloCode the names will be different from each other?
Yes, apologies as I cannot provide my original array as it's confidential. I have updated it to contain strings other than foo
|
0

I think you could do as follows

const array = ["Foo", "Bar", "John"];
Object.fromEntries(
   array
   .map((el, index) => {
     return [el, {name: el, index, type: el.constructor.name}]
   })
)

Here you can find an example snippet:

const array = ["Foo", "Bar", "John"];
const result = Object.fromEntries(
   array
   .map((el, index) => {
     return [el, {name: el, index, type: el.constructor.name}]
   })
);
console.log(result);

3 Comments

@NicoloCode, do you need any additional explanations?
Thanks for the answer. I think I may need the names without the "", including Foo, name, index, and type. Is there a simple fix for this?
It's just an object representation. It will work exactly the same as your example. If you copy the code and run it in browser console, you'll see the object properties without "" and singlequouted strings
0

your json is not valid , so if you need the valid json like this

[{"name":"Foo","index":0,"type":"string"},{"name":"Foo","index":1,"type":"string"},{"name":"Foo","index":2,"type":"string"}]

you can use this code

var result=[];
arr.forEach( (element,i )=> {
  result.push( {name:element, index:i, type: typeof element});
 });

UPDATE

for your another json

var jarr=["Foo", "Bar", "John"]

you can use this code

var result = {};
jarr.forEach((element, i) => {
  result[element] = { name: element, index: i, type: typeof element };
});

result

{
  "Foo": {
    "name": "Foo",
    "index": 0,
    "type": "string"
  },
  "Bar": {
    "name": "Bar",
    "index": 1,
    "type": "string"
  },
  "John": {
    "name": "John",
    "index": 2,
    "type": "string"
  }
}

Comments

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.