0

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.
1
  • 1
    And the problem/question is? Commented Jul 23, 2017 at 7:30

5 Answers 5

1

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);

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

1 Comment

Just great answer, if you allow me, I will quote your approach along with mine to get a solution. It's a plus one
0

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]
}

Comments

0

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);

Comments

0

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; }

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.