how to sepaarte text in object javascript


let data = 'Child 1 First Name: AlinChild 1 Gender: FemalenChild 1 Hair Color: BlondenChild 1 Hair Style: WavynChild 1 Skin Tone: TannChild 2 First Name: Morgan nChild 2 Gender: FemalenChild 2 Hair Color: BrownnChild 2 Hair Style: PonytailnChild 2 Skin Tone: LightnRelationship 1 to 2: BrothernRelationship 2 to 1: Brothern';
let target = {};

data.split('n').forEach((pair) => {
if(pair !== '') {
let splitpair = pair.split(': ');
let key = splitpair[0].charAt(0).toLowerCase() + splitpair[0].slice(1).split(' ').join('');
target[key] = splitpair[1];
}
});

console.dir(target);

www.codegrepper.com
www.stackoverflow.com

how to remove duplicate array object in javascript


let person = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"}];

function removeDuplicates(data, key) {

return [
...new Map(data.map(item => [key(item), item])).values()
]

};

console.log(removeDuplicates(person, item => item.name));

let days = ["senin","senin","selasa","selasa","rabu","kamis", "rabu"];
let fullname = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"},{name: "jane"}];

// REMOVE DUPLICATE FOR ARRAY LITERAL
const arrOne = new Set(days);
console.log(arrOne);

const arrTwo = days.filter((item, index) => days.indexOf(item) == index);
console.log(arrTwo);


// REMOVE DUPLICATE FOR ARRAY OBJECT
const arrObjOne = [...new Map(person.map(item => [JSON.stringify(item), item])).values()];
console.log(arrObjOne);

const arrObjTwo = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
console.log(arrObjTwo);

let person = [
{name: "john"},
{name: "jane"},
{name: "imelda"},
{name: "john"},
{name: "jane"}
];

const obj = [...new Map(person.map(item => [JSON.stringify(item), item])).values()];
console.log(obj);

let person = [
{name: "john"},
{name: "jane"},
{name: "imelda"},
{name: "john"},
{name: "jane"}
];

const data = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
console.log(data);

const addresses = [...]; // Some array I got from async call

const uniqueAddresses = Array.from(new Set(addresses.map(a => a.id)))
.map(id => {
return addresses.find(a => a.id === id)
})

var arrayWithDuplicates = [
{"type":"LICENSE", "licenseNum": "12345", state:"NV"},
{"type":"LICENSE", "licenseNum": "A7846", state:"CA"},
{"type":"LICENSE", "licenseNum": "12345", state:"OR"},
{"type":"LICENSE", "licenseNum": "10849", state:"CA"},
{"type":"LICENSE", "licenseNum": "B7037", state:"WA"},
{"type":"LICENSE", "licenseNum": "12345", state:"NM"}
];

function removeDuplicates(originalArray, prop) {
var newArray = [];
var lookupObject = {};

for(var i in originalArray) {
lookupObject[originalArray[i][prop]] = originalArray[i];
}

for(i in lookupObject) {
newArray.push(lookupObject[i]);
}
return newArray;
}

var uniqueArray = removeDuplicates(arrayWithDuplicates, "licenseNum");
console.log("uniqueArray is: " + JSON.stringify(uniqueArray));

www.codegrepper.com
www.stackoverflow.com

how to remove a property from an object in javascript


var ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"],
"bark": "bow-wow"
};

delete ourDog.bark;

delete obj.firstname;

var string = "Hello World";
var substring = "Hello";
if (string.indexOf(substring) != -1) {
//Code here
}

var person = {
name: "Harry",
age: 16,
gender: "Male"
};

// Deleting a property completely
delete person.age;
alert(person.age); // Outputs: undefined
console.log(person); // Prints: {name: "Harry", gender: "Male"}

www.codegrepper.com
www.stackoverflow.com