1

I have an array of objects and I am using the map function to reduce that array of objects to be an array of a single element in each object. After that I am removing the dup elements in that array. After doing that I have a array like arr = ["100", "101", "103", "104", "105"] but I want to have a key name for each element like arr =[{id: 100},{id: 101}, {id: 103},{id: 104},{id: 105}].

My initial array of objects:

const options = [
       { id: 1, value: 'chocolate', label: 'Chocolate' },
       { id: 2,value: 'strawberry', label: 'Strawberry' },
       { id: 3,value: 'vanilla', label: 'Vanilla' },
       { id: 4,value: 'vanilla', label: 'Vanilla' },
];
let testArr = options.map(a => a.value);
let uniqueTest = [...new Set(test)];

So uniqueTest gives an array like ["chocolate", "strawberry", "vanilla"]

What I want is an array with key names if that is possible.

2
  • 1
    Welcome to Stack Overflow! Please read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Commented Jul 21, 2020 at 17:24
  • 3
    Take a look at Array.prototype.map! You can then map each key to an object { id: key }. Commented Jul 21, 2020 at 17:24

4 Answers 4

3

To get the desired result, you can use Array.map, Which will give you a new array copy. And for changing from string to number you can use Number function Which will convert the string to a number.

Kindly check the below snippet

let arr = ["100", "101", "103", "104", "105"]
let output = arr.map(item => ({id: Number(item)}))
console.log(output)

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

2 Comments

I was attempting that but I was missing the parenthesis around the curly brackets. Thanks it solved my question.
@ShalinPatel happy to help, kindly accept if it worked for you. So it will be helpful for others on future references :)
1

Use map and make sure map method is returning with desired key name.

const options = [
       { id: 1, value: 'chocolate', label: 'Chocolate' },
       { id: 2,value: 'strawberry', label: 'Strawberry' },
       { id: 3,value: 'vanilla', label: 'Vanilla' },
       { id: 4,value: 'vanilla', label: 'Vanilla' },
];
let testArr = options.map(a => a.value);
let uniqueTest = [...new Set(testArr)];


const output = uniqueTest.map(key => ({key}));
console.log(output)

Comments

1

You can try using reduce as its not only used for getting the single desired output, it could be used to transform arrays into object key pairs.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

arr = ["100", "101", "103", "104", "105"]

const newArr = (acc, cv) => {
   return {...acc, ["id"]: cv};
}

const findById = arr.reduce(newArr, {});

console.log(findById)

Another example

const options = [{
    id: 1,
    value: 'chocolate',
    label: 'Chocolate'
  },
  {
    id: 2,
    value: 'strawberry',
    label: 'Strawberry'
  },
  {
    id: 3,
    value: 'vanilla',
    label: 'Vanilla'
  },
  {
    id: 4,
    value: 'vanilla',
    label: 'Vanilla'
  },
];
const newArr = (acc, cv) => {
  return { ...acc,
    [cv.label]: cv
  };
}

const findByLabel = options.reduce(newArr, {});

console.log(findByLabel)

https://jsbin.com/lixekodevi/edit?js,console

Comments

1

Here is what you want :

const options = [
       { id: 1, value: 'chocolate', label: 'Chocolate' },
       { id: 2,value: 'strawberry', label: 'Strawberry' },
       { id: 3,value: 'vanilla', label: 'Vanilla' },
       { id: 4,value: 'vanilla', label: 'Vanilla' },
];

let testArr = options.map(a => a.value);
let uniqueTest = Array.from(new Set(testArr),value=>({value}));
console.log(uniqueTest)

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.