user8226204
user8226204

Reputation:

How to make negative or positive numbers as string in an array in javascript

The scenario is, I have two arrays as below. I want to convert the values from the arrays by converting the negative elements to "", and also removing de decimals from all the elements. For the second array the same but for positives, see this example:

Case1: var arr=["1.00","-2.05","-3P","-$8M","5P","6$","-5.0%"]

    /*logic to make arr[] as below: */
        var arr2=["1","","","","5P","6$",""]
console.log(arr2)

    Case2: var arr=["1.00","-2.05","-3P","-$8M","5P","6$","-5.0%"]

    /*logic to make arr[] as below: */
        var arr2=["","-2","-3P","-$8M","","","-5%"]
console.log(arr2)


Note: I want to take string with decimal as  string without decimal  as well.

Upvotes: 0

Views: 924

Answers (5)

Amin NAIRI
Amin NAIRI

Reputation: 2504

Explaination

You can use the String.prototype.replace() method to replace unwanted decimals. Then you can use the String.prototype.charAt() method to detect if the string is contains a dash at first. Using the Array.prototype.map() method will allow you to simplify statements in your code by using some functions.

Source-code

"use strict";

const removeDecimals = string => string.replace(/\.\d*/, "");

const positiveOnly = string => string.charAt(0) === "-" ? "" : string;

const negativeOnly = string => string.charAt(0) === "-" ? string : "";

const arr = ["1.00", "-2.05", "-3P", "-$8M", "5P", "6$", "-5.0%"];

const arrPositives = arr.map(positiveOnly).map(removeDecimals);

const arrNegatives = arr.map(negativeOnly).map(removeDecimals);

// arrPositives ["1", "", "", "", "5P", "6$", ""]
// arrNegatives ["", "-2", "-3P", "-$8M", "", "", "-5%"]

Online test

JSFiddle demo.

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

You could test the first character of the string and return either the item or an empty string.

function getPositiveValues(array)  {
    return array.map(a => a.toString()[0] !== '-' ? a : '');
}

function getNegativeValues(array)  {
    return array.map(a => a.toString()[0] === '-' ? a : '');
}

var array = ["1", "-2", "-3P", "-$8M", "5P", "6$", "-5%"]

console.log(getPositiveValues(array));
console.log(getNegativeValues(array));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 0

developer_hatch
developer_hatch

Reputation: 16224

Following the example of @baao, let me say that with this little modify you can get your goal:

var arr=["1","-2","-3P","-$8M","5P","6$","-5%"];
var arr2 = arr.map(value => value.indexOf("-") == 0 ? "" : convertStr(value));

// Case 2

var arr3=["1","-2","-3P","-$8M","5P","6$","-5%"];
var arr4 = arr.map(value => value.indexOf("-") == 0 ? convertStr(value) : "");

console.log(arr2,arr4);
function convertStr(str){
  var values = str.split(",")
  return  values[0]
}

Upvotes: 0

baao
baao

Reputation: 73231

A simple map and indexOf check will do this

var arr=["1","-2","-3P","-$8M","5P","6$","-5%"];
var arr2 = arr.map(e => e.indexOf("-") > -1 ? "" : e);

// Case 2

var arr3=["1","-2","-3P","-$8M","5P","6$","-5%"];
var arr4 = arr.map(e => e.indexOf("-") > -1 ? e : "");

console.log(arr2,arr4);

Upvotes: 1

Trash Can
Trash Can

Reputation: 6814

Something like this should do

// Case 1
var arr11=["1","-2","-3P","-$8M","5P","6$","-5%"]
var arr12 = arr11.map(e => /^-/.test(e) ? '' : e);
console.log(arr12);


// Case 2
var arr21 =["1","-2","-3P","-$8M","5P","6$","-5%"]
var arr22 = arr21.map(e => /^-/.test(e) ? e : '');
console.log(arr22);

Upvotes: 0

Related Questions