如何用lodash替换数组中的十进制数

时间:2017-11-04 14:38:02

标签: javascript arrays regex lodash

我想用空字符串[ 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 1.5, 1.55, 1.6, 1.65, 1.7, 1.75, 1.8, 1.85, 1.9, 1.95, 2, 2.05, 2.1, 2.15, 2.2, 2.25, 2.3, 2.35, 2.4, 2.45, 2.5, 2.55, 2.6, 2.65, 2.7, 2.75, 2.8, 2.85, 2.9, 2.95, 3, 3.05, 3.1, 3.15, 3.2, 3.25, 3.3, 3.35, 3.4, 3.45, 3.5, 3.55, 3.6, 3.65, 3.7, 3.75, 3.8, 3.85, 3.9, 3.95, 4, 4.05, 4.1, 4.15, 4.2, 4.25, 4.3, 4.35, 4.4, 4.45, 4.5, 4.55, 4.6, 4.65, 4.7, 4.75, 4.8, 4.85, 4.9, 4.95, 5, 5.05, 5.1, 5.15, 5.2, 5.25, 5.3, 5.35, 5.4, 5.45, 5.5, 5.55, 5.6, 5.65, 5.7, 5.75, 5.8, 5.85, 5.9, 5.95, 6, 6.05, 6.1, 6.15, 6.2, 6.25, 6.3, 6.35, 6.4, 6.45, 6.5, 6.55, 6.6, 6.65, 6.7, 6.75, 6.8, 6.85, 6.9, 6.95, 7, 7.05, 7.1, 7.15, 7.2, 7.25, 7.3, 7.35] 替换数组列表中的十进制数。我怎么能用lodash做到这一点?

这是我的数组示例:

["0", "", "", ..., "", "", "1", "", "", ..., "", "", "2", etc... ]

我想要的结果:

mov byte[string_out + ebx], al

2 个答案:

答案 0 :(得分:2)

转换为字符串后,只需检查是否有点

    public class MainActivity extends Activity implements
        AdapterView . OnItemSelectedListener {

    EditText Name,Email,Number;
    Button submit;
    String [] countryNames ={ "India" ,"Australia" ,"Pakistan" ,"Afghanistan" ,"US","Canada","Israel" };
    @Override
    protected void onCreate ( Bundle savedInstanceState ) {
        super . onCreate ( savedInstanceState );
        setContentView ( R . layout . activity_main );
        Name = (EditText)findViewById(R.id.name);
        Email = (EditText)findViewById(R.id.email);
        Number = (EditText)findViewById(R.id.number);
        submit = (Button)findViewById(R.id.submit);
        final Spinner spin = (Spinner) findViewById(R.id.simplespinner);
        spin.setOnItemSelectedListener(this);
        final CheckBox BEBox = (CheckBox)findViewById(R.id.BEBox);


        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = Name.getText().toString();
                String email = Email.getText().toString();
                String number = Number.getText().toString();
                String spinText = spin.getSelectedItem().toString();

                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                intent.putExtra("Name",name);
                intent.putExtra("Email",email);
                intent.putExtra("Number",number);
                intent.putExtra("spin",spinText);
                String checkboxString = null;
                if (BEBox.isChecked()) checkboxString = (String) BEBox.getText();
                intent.putExtra("checkbox",checkboxString);

                startActivity(intent);

            }
        });




        ArrayAdapter aa = new ArrayAdapter ( this , android . R . layout . simple_spinner_item , countryNames );
        aa . setDropDownViewResource ( android . R . layout . simple_spinner_dropdown_item );

        spin . setAdapter ( aa );
    }

    @Override
    public void onItemSelected ( AdapterView <?> arg0 , View arg1 , int position , long id ) {
        Toast . makeText ( getApplicationContext (), countryNames [ position ], Toast . LENGTH_LONG ). show ();
    }
    @Override
    public void onNothingSelected ( AdapterView <?> arg0 ) {

    }
}

答案 1 :(得分:2)

只想添加来自@charlietfl的回答。这是另一种变化。我相信它的性能略高,因为每个tick都没有字符串创建操作。

&#13;
&#13;
var data = [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 1.5, 1.55, 1.6, 1.65, 1.7, 1.75, 1.8, 1.85, 1.9, 1.95, 2, 2.05, 2.1, 2.15, 2.2, 2.25, 2.3, 2.35, 2.4, 2.45, 2.5, 2.55, 2.6, 2.65, 2.7, 2.75, 2.8, 2.85, 2.9, 2.95, 3, 3.05, 3.1, 3.15, 3.2, 3.25, 3.3, 3.35, 3.4, 3.45, 3.5, 3.55, 3.6, 3.65, 3.7, 3.75, 3.8, 3.85, 3.9, 3.95, 4, 4.05, 4.1, 4.15, 4.2, 4.25, 4.3, 4.35, 4.4, 4.45, 4.5, 4.55, 4.6, 4.65, 4.7, 4.75, 4.8, 4.85, 4.9, 4.95, 5, 5.05, 5.1, 5.15, 5.2, 5.25, 5.3, 5.35, 5.4, 5.45, 5.5, 5.55, 5.6, 5.65, 5.7, 5.75, 5.8, 5.85, 5.9, 5.95, 6, 6.05, 6.1, 6.15, 6.2, 6.25, 6.3, 6.35, 6.4, 6.45, 6.5, 6.55, 6.6, 6.65, 6.7, 6.75, 6.8, 6.85, 6.9, 6.95, 7, 7.05, 7.1, 7.15, 7.2, 7.25, 7.3, 7.35]
.map((e) => {
  return e % 1 === 0 ? e : '';
});
console.log(data)
&#13;
&#13;
&#13;