javascript how do you copy an object code example

Example 1: remove a particular element from array

var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car");//get  "car" index
//remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red","blue","green"]

Example 2: copy object javascript

var x = {key: 'value'}
var y = JSON.parse(JSON.stringify(x))

//this method actually creates a reference-free version of the object, unlike the other methods
//If you do not use Dates, functions, undefined, regExp or Infinity within your object

Example 3: make copy of object javascript

var x = {key: 'value'}
var y = JSON.parse(JSON.stringify(x))

//If you do not use Dates, functions, undefined, regExp or Infinity within your object

Example 4: Java arraylist if you don't want to use add()

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ListView myListView = (ListView) findViewById(R.id.myListView);

        final ArrayList<String> myFriendsList = new ArrayList<String>(asList("Friend1", "Friend2", "Friend3", "Friend4", "Friend5"));

        // set the layout of the list
        ArrayAdapter<String> friendsArrayAdapter = new ArrayAdapter<String>(this,
                                                android.R.layout.simple_list_item_1, myFriendsList);

        // connect adapter to myFriendsList
        myListView.setAdapter(friendsArrayAdapter);

        // set onClickListener
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(MainActivity.this, myFriendsList.get(position), Toast.LENGTH_SHORT).show();
            }
        });




    }
}