0

I have an array that I'm currently breaking down with the map function in order to only get the option index of the original array. This works, but now I'm wanting to strip this down and only get the first 2 numerical digits from that option index (or more specifically, anything in front of the '-' in that value)

What's the best way to strip out only those digits while mapping it the way I currently am?

var vm = 
new Vue({
  el: "#app",
  data: {
    options: [
      {id:100, option:"1 - John Doe"},
      {id:200, option:"2 - Jane Doe"}
    ]
  },
  methods: {
    getNames(){
      
      let names = this.options.map(option => {
        return {
          id: option.option
        }
      });
      
      console.log(names);
    }
  }
  
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button @click="getNames()">TEST</button>
</div>

2
  • Welcome to SO! What did you try? Please give an minimal reproducible example (for the code you tried and the wanted example). Commented Feb 3, 2022 at 6:18
  • Please clarify: (a) Should the array keep its nested structure (i.e. objects containing only id property) ? (b) Should the returned number be of type String or Number ? Commented Feb 3, 2022 at 7:00

4 Answers 4

1

Pure JavaScript to Return only numbers from string.

Example for returned array:

[
  {id: "1"},
  {id: "2"}
]

Solution

Use replace with a regex to remove any non-numeric from the string - return only numbers (as string).

The regex is applied to all occurrences using the global flag behind surrounding slashes //g. The pattern inside slashes can be any of those equivalents to keep the numerical digits only:

  • \D matches all non-number characters (meta-character)
  • [^0-9] matches all non-number characters (inversed character-range)

Demo:

let options =  [
  {id:100, option:"1 - John Doe"},
  {id:200, option:"2 - Jane Doe"}
];

// keep only the first digits before hyphen
let names = options.map(o => {
    return {id: o.option.replace(/[^0-9]/g, "")};
});

console.log(names);

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

1 Comment

This works great, thanks! I had previously tried a method from a vue regex library but I think it was overcomplicating things
1

You can edit your map callback to get the first part of the string:

let options =  [
  {id:100, option:"1 - John Doe"},
  {id:200, option:"2 - Jane Doe"}
];

let names = options.map(option => {
    let opt = option.option;
    id = opt.split(' - ')[0];
    return { id };
});

console.log(names);

Comments

0
const names = this.options.map(option => {
    const text = option.option // Get raw text from 'option'
    const parts = text.split('-') // Split text string by the '-' (returns an array, E.g. ["1 ", " John Doe"])
    return parts[0] // The first element of 'parts' is the result you are looking for (note it is a string, not a number)
})

Comments

0

You can use split method of string to split the string by " - " and get 1st part(the numerical digits). Additionally, you can use parseInt function, to convert string to integers.

var vm = 
new Vue({
  el: "#app",
  data: {
    options: [
      {id:100, option:"1 - John Doe"},
      {id:200, option:"2 - Jane Doe"}
    ]
  },
  methods: {
    getNames(){
      
      let names = this.options.map(option => {
        return {
          id: parseInt(option.option.split(" - "))
        }
      });
      
      console.log(names);
    }
  }
  
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button @click="getNames()">TEST</button>
</div>

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.