user3711357
user3711357

Reputation: 1625

Clone the dynamic object and make reset (empty) its value

Have data object as:

var data = [{id:1, name:'t1'},{id:2, name:'t2'},{id:1, name:'t3'}]

this can contain different JSON object in array.

var data = [{value:1, key:'t1'}]

How to clone JSON object and reset its value. like,{id:1, name:'t1'} to {id:0, name:''} and {value:1, key:'t1'} to {value:0, key:''}


I tried to do like:

function getNew()
{
return {id:0, name:''};   //here JSON can be vary.

}

But problem is, its static reset , here JSON object can be vary. I just need to get empty JSON object that is inline 'data' collection above.

Upvotes: 0

Views: 76

Answers (3)

darklightcode
darklightcode

Reputation: 2772

The .pop answer have been provided, so i present two more alternatives:

UPDATE : This will reset your keys, even if data is susceptible to change.

var data = [{
    value: 1,
    key: 't1',
    'obj': {
      name: 1
    },
    'arr': [1, 2, 3, 4, 5],
    'test': "undefined"
  },
  {
    value: 333,
    'arr': 'no arr',
    'some': "undefined"
  }
];

var resetJSON = function(arr) {

  var defaultTypes = {
    'string': '',
    'number': 0,
    'object': {},
    'array': [],
    'function': function() {},
    'boolean': false
  };

  var detectType = function(value) {

    // object | array
    // function
    // string
    // number
    // boolean
    // undefined ( as string )
    // null

    try {

      let type = "";

      if (typeof value === "string") {

        type = "string";

      }

      if (value === void 0) {

        type = "undefined";

      }

      if (typeof value === "number") {

        type = "number";

      }

      if (typeof value === "boolean") {

        type = "boolean";

      }

      if (typeof value === "object" && typeof(value.length) === "number") {

        type = 'array';

      }
      if (typeof value === "object" && typeof(value.length) === "undefined") {

        type = 'object';

      }

      return type;

    } catch (e) {

      return "null";

    }

  }

  arr = arr.map(function(item) {

    for (let i in item) {

      item[i] = defaultTypes[detectType(item[i])];

    }

    return item;

  });

  return arr;

}

console.log('Input data:');
console.log('----------');
console.log(data);
console.log('----------');
console.log('Output data:');
console.log('----------');
console.log(resetJSON(data));

1. Target last index:

var data = [{
  id: 1,
  name: 't1'
}, {
  id: 2,
  name: 't2'
}, {
  id: 1,
  name: 't3'
}];

data = data[data.length - 1];

console.log(data);

2. Reduce the array using .reduce

var data = [{
  id: 1,
  name: 't1'
}, {
  id: 2,
  name: 't2'
}, {
  id: 1,
  name: 't3'
}].reduce((a, i) => i);

console.log(data);

Upvotes: 1

ABC
ABC

Reputation: 2148

Some just list's each item in the array. If you wanted the results of array1, arr1.forEach would return 1, 2, 3, 4, 5 each item in the array, one by one until the end.

Includes means if arr2.includes(1) we are seeing the array2 has the item 1. So I used !arr2.includes(item) to see if it did not contain it.

Last example, gets the last result in array2. Then changes it to that value + 1.

I give you many examples buddy.

var arr1 = [1, 2, 3, 4, 5]
        var arr2 = [1, 2, 3, 4, 6]
        var final = []
        arr1.forEach(item => {
            if (!arr2.includes(item)) {
                final.push(item)
                final.push({new: item+2})
                console.log('A: Array 2 does not have', item)
            }
        })
        console.log("New array", final)
        // Or
        function check(a, b, callback) {
            var x = []
            a.forEach(item => {
            if (!b.includes(item)) {
                x.push(item)
                x.push({new: item+2}) 
                console.log('B: Array 2 does not have', item)
                }
                
            })
           callback(x)
        }
        check(arr1, arr2, function(response) {
          console.log("Example two new array", response)
        })
        // Or last
        var y = []
        for (var i = 0; i < arr2.length; i++) {
           if (i == arr2.length-1) {
              y.push({"new": arr2[i]+1})
           }
        }
        console.log("C: Last", y)
        // Another
        var data = [{one: "1"}]
        function last(x, callback) {
           var finalz = x[x.length - 1]; // Change value
           callback(finalz)
        }
        last(data, function(response) {
          var finalb = []
           for (property in response) {
              finalb.push({"new_last_example": property+1000})
           }
           console.log(finalb)
        })

Upvotes: 0

S--
S--

Reputation: 398

This will get you the last value:

var data = [{id:1, name:'t1'},{id:2, name:'t2'},{id:1, name:'t3'}];
var newData = data.pop();

Upvotes: 0

Related Questions