Related
I'm trying to manipulate this sample array of objects.
[ { name: 'John Wilson',
id: 123,
classes: ['java', 'c++']},
{ name: 'John Wilson',
id: 123,
classes: 'uml'},
{ name: 'Jane Smith',
id: 321,
classes: 'c++'} ]
What I need to do is to merge objects with the same 'id', concatenating 'classes' and keeping one 'name'.
The result should be:
[ { name: 'John Wilson',
id: 123,
classes: ['java', 'c++', 'uml']},
{ name: 'Jane Smith',
id: 321,
classes: 'c++'} ]
I tried using .merge but it doesn't concatenate the values from 'classes', it just keeps the values from the last equal object.
What is the simplest way to do that, using lodash?
The function you're looking for is _.uniqWith, with a special twist which I will explain in a minute.
_.uniqWith is a lot like _.uniq in that it generates a unique array, but it allows you to pass your own custom comparator function that will be called to determine what counts as "equality."
Sane programmers would understand that this comparator should be side-effect free. The way this code works is by breaking that rule, and using a comparison function that does extra magic behind the scenes. However, this results in very concise code that will work no matter how many of these objects are in your array, so I feel like the transgression is well-justified.
I named the comparator function compareAndMerge so as not to hide its impure nature. It will merge both classes arrays and update the relevant property on both objects, but only if their id values are identical.
function merge(people) {
return _.uniqWith(people, compareAndMerge)
}
function compareAndMerge(first, second) {
if (first.id === second.id) {
first.classes = second.classes = [].concat(first.classes, second.classes)
return true
}
return false
}
var people = [{
name: 'John Wilson',
id: 123,
classes: ['java', 'c++']
}, {
name: 'John Wilson',
id: 123,
classes: 'uml'
}, {
name: 'Jane Smith',
id: 321,
classes: 'c++'
}]
console.log(merge(people))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
An aside: You were missing square brackets around your original classes lists. I made sure that the code above doesn't care whether or not the classes property holds a single string or an array of strings, though, just in case.
Using ES6 you can do so with a Map to hold the unique values, Array#reduce to populate it, and the spread operator with Map#values to convert it back to array:
const arr = [{"name":"John Wilson","id":123,"classes":["java","c++"]},{"name":"John Wilson","id":123,"classes":"uml"},{"name":"Jane Smith","id":321,"classes":"c++"}];
const result = [...arr.reduce((hash, { id, name, classes }) => {
const current = hash.get(id) || { id, name, classes: [] };
classes && (current.classes = current.classes.concat(classes));
return hash.set(id, current);
}, new Map).values()];
console.log(result);
Not sure using lodash... here's a way to do it with normal JS:
var combined = arr.reduce(function(a, item, idx) {
var found = false;
for (var i = 0; i < a.length; i++) {
if (a[i].id == item.id) {
a[i].classes = a[i].classes.concat(item.classes);
found = true;
break;
}
}
if (!found) {
a.push(item);
}
return a;
}, []);
Fiddle: https://jsfiddle.net/6zwr47mt/
use _.mergeWith to set merging customizer
_.reduce(data, function(result, item) {
item = _.mergeWith(
item,
_.find(result, {id: item.id}),
function(val, addVal) {
return _.isArray(val) ? _.concat(val, addVal) : val;
});
result = _.reject(result, {id: item.id})
return _.concat(result, item);
}, []);
The following algorithm is not the best one but at least I know what it does :-)
console.log(clean(data));
function clean (data) {
var i, x, y;
var clean = [];
var m = clean.length;
var n = data.length;
data.sort((x, y) => x.id - y.id);
for (i = 0; i < n; i++) {
y = data[i];
if (i == 0 || x.id != y.id) {
clean.push(x = clone(y)), m++;
} else {
clean[m - 1] = merge(x, y);
}
}
return clean;
}
function clone (x) {
var z = {};
z.id = x.id;
z.name = x.name;
z.classes = x.classes.slice();
return z;
}
function merge (x, y) {
var z = {};
z.id = x.id;
z.name = x.name;
z.classes = unique(
x.classes.concat(y.classes)
);
return z;
}
function unique (xs) {
var i, j, n;
n = xs.length;
for (i = 1; i < n; i++) {
j = 0; while (j < i && xs[i] !== xs[j]) j++;
if (j < i) swap(xs, i, n - 1), i--, n--;
}
return xs.slice(0, n);
}
function swap (xs, i, j) {
var x = xs[i];
xs[i] = xs[j];
xs[j] = x;
}
<script>
var data = [{
id: 123,
name: 'John Wilson',
classes: ['java', 'c++']
}, {
id: 123,
name: 'John Wilson',
classes: ['uml', 'java']
}, {
id: 321,
name: 'Jane Smith',
classes: ['c++']
}];
</script>
What is the best way to fill in missing properties in an array of objects, such as this example:
[
{
name: 'Tom',
number: '01234 567 890',
website: 'http://www.tom.com'
},
{
name: 'Richard',
number '07777 666 555'
},
{
name: 'Harry',
website: 'http://www.harry.com'
}
]
I need to add the missing properties with a null value, so that when I pass this array on to be rendered in something such as a HTML table or CSV file, everything lines up correctly. I was thinking of passing over the array twice, once to get all the possible properties, and a second time to add those missing properties with a null value to each object where it doesn't exist. Is there a better way to do this?
EDIT: I won't know what the keys are until I have the data, it's coming from an API and the keys are not always requested explicitly.
My final solution
Thanks all, it seems the two pass approach is indeed the best approach. After I started to write this using the examples provided, I realised that the order of the properties wasn't being maintained. This is how I achieved filling in the missing props, and maintaining the correct order. Any suggestions for potential improvements are welcome.
var fillMissingProps = function(arr) {
// build a list of keys in the correct order
var keys = [];
arr.forEach(function(obj) {
var lastIndex = -1;
Object.keys(obj).forEach(function(key, i) {
if (keys.includes(key)) {
// record the position of the existing key
lastIndex = keys.lastIndexOf(key);
if (lastIndex < i) {
// this key is in the wrong position so move it
keys.splice(i, 0, keys.splice(lastIndex, 1)[0]);
lastIndex = i;
}
} else {
// add the new key in the correct position
// after the previous existing key
lastIndex++;
keys.splice(lastIndex, 0, key);
}
});
});
// build a template object with all props set to null
// and in the correct position
var defaults = {};
keys.forEach(function(key) {
defaults[key] = null;
});
// and update the array by overwriting each element with a
// new object that's built from the template and the original object
arr.forEach(function(obj, i, arr) {
arr[i] = Object.assign({}, defaults, obj);
});
return arr;
};
/** TEST **/
var currentArray = [
{
website: 'http://www.unknown.com'
},
{
name: 'Tom',
number: '01234 567 890',
website: 'http://www.tom.com'
},
{
title: 'Mr',
name: 'Richard',
gender: 'Male',
number: '04321 666 555'
},
{
id: '003ABCDEFGHIJKL',
name: 'Harry',
website: 'http://www.harry.com',
mobile: '07890 123 456',
city: 'Brentwood',
county: 'Essex'
}
];
var newArray = fillMissingProps(currentArray);
for (var i = 0; i < newArray.length; i++) {
for (var prop in newArray[i]) {
console.log(prop + ": " + newArray[i][prop]);
}
console.log('---------');
}
Given that you don't know apriori which keys are supposed to exist, you have no choice but to iterate over the array twice:
// build a map of unique keys (with null values)
var keys = {}
array.forEach(el => Object.keys(el).forEach(k => keys[k] = null));
// and update the array by overwriting each element with a
// new object that's built from the null map and the original object
array.forEach((el, ix, a) => a[ix] = Object.assign({}, keys, el));
Use Array.prototype.map():
const arr = [
{
name: 'Tom',
number: '01234 567 890',
website: 'http://www.tom.com',
},
{
name: 'Richard',
number: '07777 666 555',
},
{
name: 'Harry',
website: 'http://www.harry.com',
},
];
const newArr = arr.map(x => (
arr.map(x => Object.keys(x))
.reduce((a, b) =>
(b.forEach(z => a.includes(z) || a.push(z)), a)
)
.forEach(
y => (x[y] = x.hasOwnProperty(y) ? x[y] : null)
), x)
);
console.log(newArr);
Here is a more interesting answer, its a tad fun one but it will build up your objects on the fly as new properties appear:
var currentArray = [
{
name: 'Tom',
number: '01234 567 890',
website: 'http://www.tom.com'
},
{
name: 'Richard',
number: '07777 666 555'
},
{
name: 'Harry',
website: 'http://www.harry.com'
}
]
var newArray = []
function NewObject() {
}
for(var i = 0; i < currentArray.length; i++){
var nObj = new NewObject();
for(var prop in currentArray[i]){
if(!NewObject.hasOwnProperty(prop))
NewObject.prototype[prop] = null;
nObj[prop]=currentArray[i][prop];
}
newArray.push(nObj);
}
for(var i = 0; i < newArray.length; i++){
for(var prop in newArray[i]){
console.log(prop+ ": "+newArray[i][prop]);
}
console.log('---------');
}
It builds new objects from the ones you provide and adds new properties to the objects if they don't exist already.
This idea was more for curiosities sake tho so any comments would be interesting :)
You can get all keys and set all keys using for..of loop, .map() to iterate all Object.keys(), redefine original array
var arr = [{
name: 'Harry',
website: 'http://www.harry.com'
},{
name: 'Tom',
number: '01234 567 890',
website: 'http://www.tom.com'
}, {
name: 'Richard',
number: '07777 666 555'
}];
for (var obj of arr) {
for (var key of Object.keys(obj)) {
arr = arr.map(o => (o[key] = o[key] || null, o))
}
};
console.log(arr);
Something like this could work:
for (var i = 0; i < arrayLength; i++) {
yourArray[i].name = yourArray[i].name || null;
yourArray[i].number = yourArray[i].number || null;
yourArray[i].website= yourArray[i].website|| null;
}
What I'm trying to solve is: preserving the order of my array of Ids with $in using this suggested method (mapReduce):
Does MongoDB's $in clause guarantee order
I've done my homework, and saw it's ideal to convert them to strings:
Comparing mongoose _id and strings.
Code:
var dataIds = [ '57a1152a4d124a4d1ad12d80',
'57a115304d124a4d1ad12d81',
'5795316dabfaa62383341a79',
'5795315aabfaa62383341a76',
'57a114d64d124a4d1ad12d7f',
'57953165abfaa62383341a78' ];
CollectionSchema.statics.all = function() {
var obj = {};
//adds dataIds to obj.scope as inputs , to be accessed in obj.map
obj.scope = {'inputs': dataIds};
obj.map = function() {
//used toString method as suggested in other SO answer, but still get -1 for Id.
var order = inputs.indexOf(this._id.toString());
emit(order, {
doc : this
});
};
obj.reduce = function() {};
obj.out = {inline: 1};
obj.query = {"_id": {"$in": dataIds } };
obj.finalize = function(key, value) {
return value;
};
return Product
.mapReduce(obj)
.then(function(products){
console.log('map products : ', products)
})
};
This is what I keep getting back for my console.log in the products promise :
[{ _id: -1, value: null } ]
Which, leads me to believe it's not able to match the ObjectId from this, with an index of dataIds. However, if I just use the $in clause within a .find(), the correct products are returned -- but, in the incorrect order.
Update:
getting unexpected behavior with this.
obj.map = function() {
for(var i = 0; i < inputs.length; i++){
if(inputs[i] == this._id.toString()){
}
emit(inputs[i], this);
}
};
emits:
[ { _id: '5795315aabfaa62383341a76', value: null },
{ _id: '57953165abfaa62383341a78', value: null },
{ _id: '5795316dabfaa62383341a79', value: null },
{ _id: '57a114d64d124a4d1ad12d7f', value: null },
{ _id: '57a1152a4d124a4d1ad12d80', value: null },
{ _id: '57a115304d124a4d1ad12d81', value: null } ]
obj.map = function() {
for(var i = 0; i < inputs.length; i++){
if(inputs[i] == this._id.toString()){
var order = i;
}
emit(this._id.toString(), this);
}
};
emits:
[ { _id: 'ObjectId("5795315aabfaa62383341a76")', value: null },
{ _id: 'ObjectId("57953165abfaa62383341a78")', value: null },
{ _id: 'ObjectId("5795316dabfaa62383341a79")', value: null },
{ _id: 'ObjectId("57a114d64d124a4d1ad12d7f")', value: null },
{ _id: 'ObjectId("57a1152a4d124a4d1ad12d80")', value: null },
{ _id: 'ObjectId("57a115304d124a4d1ad12d81")', value: null } ]
Now, how do I get rid of the ObjectId() wrapper? Preferably, something more clean than str.slice(), which would work -- However, I feel there must be a more "mongo" / safer way of converting the Id.
I checked out the docs, but it only mentions the toString() method, which does not seem to be working correctly within map: https://docs.mongodb.com/manual/reference/method/ObjectId.toString/
figured it out:
obj.map = function() {
for(var i = 0; i < inputs.length; i++){
if(this._id.equals(inputs[i])) {
var order = i;
}
}
emit(order, {doc: this});
};
I'm building a xml to json function which converts our XML structure to a specifically formatted JSON.
I've tried many libraries, and ultimately, I've settled on just turning the XML into a DOM tree that I can walk and convert to JSON myself because the libraries don't provide the format I need.
So here's an example xml document:
var xml = '<document>' +
'<divisions>' +
'<division id="123" division="foo">' +
'<departments>' +
'<department id="456" department="bar"/>'+
'<department id="678" department="bar"/>'+
'</departments>' +
'</division>' +
'</divisions>' +
'<roles>' +
'<role id="123" name="foo"/>' +
'<role id="123" name="foo"/>' +
'</roles>' +
'</document>';
Desired output:
{ divisions: [ { id: '123', division: 'foo', departments: [ { id: '456', department: 'bar' } ] } ], roles: [ { id: '123', name: 'foo'}, { id: '123', name: 'foo'} ] }
Here's my initial stab at it:
var DOMParser = require('xmldom').DOMParser;
function XMLtoJSON(xml) {
var json = {};
var dom = new DOMParser().parseFromString(xml).childNodes[0];
function process(nodes, parent) {
var node, name, hasChildren;
for(var i = 0, l = nodes.length; i < l; i++) {
node = nodes[i];
name = node.tagName;
hasChildren = node.hasChildNodes();
if(!parent) {
json[name] = [];
}
if(node.hasAttributes()) {
var attributes = node.attributes, obj = {};
for(var x = 0, al = attributes.length; x < al; x++) {
obj[attributes[x].name] = attributes[x].value;
if(!parent) {
json[name].push(obj);
}
}
}
if(hasChildren) {
process(nodes[i].childNodes);
}
}
}
process(dom.childNodes);
return json;
}
console.log( XMLtoJSON(xml) );
Currently, this will output:
{ divisions: [],
division:
[ { id: '123', division: 'foo' },
{ id: '123', division: 'foo' } ],
departments: [],
department:
[ { id: '678', department: 'bar' },
{ id: '678', department: 'bar' } ],
roles: [],
role: [ { id: '123', name: 'foo' }, { id: '123', name: 'foo' } ] }
Note: I don't want to the parent document in my json, thus the initial parseFromString(xml).childNodes[0];
I'm wondering if anyone can help get me a little closer. Specifically, I'm having trouble wrapping my head around how to handle embedded collections (departments is an array within divisions). I've been working on this for a few hours and the recursion keeps tripping me up.
Sorry it took so long to answer. This was kind of hard one. Following code works at least with you current example XML. Tricky part is to decide which elements are arrays and which elements are not. The logic here is that everything with attributes is object and everything else is array.
function DOMToObject (node, obj, isArray) {
var child = node.firstChild,
attributes,
newObj,
i,
j,
k = 0;
if (!obj) {
obj = {};
}
while (child) {
attributes = child.attributes;
if (attributes && attributes.length) {
newObj = {};
DOMToObject(child, newObj);
for (i = 0, j = attributes.length; i < j; i++) {
newObj[attributes[i].name] = attributes[i].value;
}
} else {
newObj = [];
DOMToObject(child, newObj, true);
}
if (isArray) {
obj[k] = newObj;
k += 1;
} else {
obj[child.nodeName] = newObj;
}
child = child.nextSibling;
}
return obj;
};
var JSON = JSON.stringify(DOMToObject(dom.firstChild));
If I have the following array of objects:
[ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]
Is there a way to loop through the array to check whether a particular username value already exists and if it does do nothing, but if it doesn't to add a new object to the array with said username (and new ID)?
Thanks!
I've assumed that ids are meant to be unique here. some is a great function for checking the existence of things in arrays:
const arr = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }];
function add(arr, name) {
const { length } = arr;
const id = length + 1;
const found = arr.some(el => el.username === name);
if (!found) arr.push({ id, username: name });
return arr;
}
console.log(add(arr, 'ted'));
This small snippets works for me..
const arrayOfObject = [{ id: 1, name: 'john' }, {id: 2, name: 'max'}];
const checkUsername = obj => obj.name === 'max';
console.log(arrayOfObject.some(checkUsername))
if you have array of elements like ['john','marsh'] then we can do some thing like this
const checkUsername = element => element == 'john';
console.log(arrayOfObject.some(checkUsername))
It's rather trivial to check for existing username:
var arr = [{ id: 1, username: 'fred' },
{ id: 2, username: 'bill'},
{ id: 3, username: 'ted' }];
function userExists(username) {
return arr.some(function(el) {
return el.username === username;
});
}
console.log(userExists('fred')); // true
console.log(userExists('bred')); // false
But it's not so obvious what to do when you have to add a new user to this array. The easiest way out - just pushing a new element with id equal to array.length + 1:
function addUser(username) {
if (userExists(username)) {
return false;
}
arr.push({ id: arr.length + 1, username: username });
return true;
}
addUser('fred'); // false
addUser('bred'); // true, user `bred` added
It will guarantee the IDs uniqueness, but will make this array look a bit strange if some elements will be taken off its end.
There could be MULTIPLE POSSIBLE WAYS to check if an element(in
your case its Object) is present in an array or not.
const arr = [
{ id: 1, username: 'fred' },
{ id: 2, username: 'bill' },
{ id: 3, username: 'ted' },
];
let say you want to find an object with id = 3.
1. find:
It searches for an element in an array and if it finds out then it returns that element else return undefined. It returns the value of the first element in the provided array that satisfies the provided testing function. reference
const ObjIdToFind = 5;
const isObjectPresent = arr.find((o) => o.id === ObjIdToFind);
if (!isObjectPresent) { // As find return object else undefined
arr.push({ id: arr.length + 1, username: 'Lorem ipsum' });
}
2. filter:
It searches for elements in an array and filters out all element that matches the condition. It returns a new array with all elements and if none matches the condition then an empty array. reference
const ObjIdToFind = 5;
const arrayWithFilterObjects= arr.filter((o) => o.id === ObjIdToFind);
if (!arrayWithFilterObjects.length) { // As filter return new array
arr.push({ id: arr.length + 1, username: 'Lorem ipsum' });
}
3. some:
The some() method tests whether at least one element is present in an array that passes the test implemented by the provided function. It returns a Boolean value. reference
const ObjIdToFind = 5;
const isElementPresent = arr.some((o) => o.id === ObjIdToFind);
if (!isElementPresent) { // As some return Boolean value
arr.push({ id: arr.length + 1, username: 'Lorem ipsum' });
}
This is what I did in addition to #sagar-gavhane's answer
const newUser = {_id: 4, name: 'Adam'}
const users = [{_id: 1, name: 'Fred'}, {_id: 2, name: 'Ted'}, {_id: 3, name:'Bill'}]
const userExists = users.some(user => user.name === newUser.name);
if(userExists) {
return new Error({error:'User exists'})
}
users.push(newUser)
I think that, this is the shortest way of addressing this problem. Here I have used ES6 arrow function with .filter to check the existence of newly adding username.
var arr = [{
id: 1,
username: 'fred'
}, {
id: 2,
username: 'bill'
}, {
id: 3,
username: 'ted'
}];
function add(name) {
var id = arr.length + 1;
if (arr.filter(item=> item.username == name).length == 0){
arr.push({ id: id, username: name });
}
}
add('ted');
console.log(arr);
Link to Fiddle
Let's assume we have an array of objects and you want to check if value of name is defined like this,
let persons = [ {"name" : "test1"},{"name": "test2"}];
if(persons.some(person => person.name == 'test1')) {
... here your code in case person.name is defined and available
}
try this
first method using some
let arr = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }];
let found = arr.some(ele => ele.username === 'bill');
console.log(found)
second method using includes, map
let arr = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }];
let mapped = arr.map(ele => ele.username);
let found = mapped.includes('bill');
console.log(found)
You could prototype your array to make it more modular, try something like this
Array.prototype.hasElement = function(element) {
var i;
for (i = 0; i < this.length; i++) {
if (this[i] === element) {
return i; //Returns element position, so it exists
}
}
return -1; //The element isn't in your array
};
And you can use it as:
yourArray.hasElement(yourArrayElement)
Accepted answer can also be written in following way using arrow function on .some
function checkAndAdd(name) {
var id = arr.length + 1;
var found = arr.some((el) => {
return el.username === name;
});
if (!found) { arr.push({ id: id, username: name }); }
}
Here is an ES6 method chain using .map() and .includes():
const arr = [ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]
const checkForUser = (newUsername) => {
arr.map(user => {
return user.username
}).includes(newUsername)
}
if (!checkForUser('fred')){
// add fred
}
Map over existing users to create array of username strings.
Check if that array of usernames includes the new username
If it's not present, add the new user
I like Andy's answer, but the id isn't going to necessarily be unique, so here's what I came up with to create a unique ID also. Can be checked at jsfiddle too. Please note that arr.length + 1 may very well not guarantee a unique ID if anything had been removed previously.
var array = [ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' } ];
var usedname = 'bill';
var newname = 'sam';
// don't add used name
console.log('before usedname: ' + JSON.stringify(array));
tryAdd(usedname, array);
console.log('before newname: ' + JSON.stringify(array));
tryAdd(newname, array);
console.log('after newname: ' + JSON.stringify(array));
function tryAdd(name, array) {
var found = false;
var i = 0;
var maxId = 1;
for (i in array) {
// Check max id
if (maxId <= array[i].id)
maxId = array[i].id + 1;
// Don't need to add if we find it
if (array[i].username === name)
found = true;
}
if (!found)
array[++i] = { id: maxId, username: name };
}
i did try the above steps for some reason it seams not to be working for me but this was my final solution to my own problem just maybe helpful to any one reading this :
let pst = post.likes.some( (like) => { //console.log(like.user, req.user.id);
if(like.user.toString() === req.user.id.toString()){
return true
} } )
here post.likes is an array of users who liked a post.
Greatly simplifying my previous solutions here and providing better performance by not iterating over the entire array unnecessarily before checking for the existence of the specified ID.
This should be the simplest solution (I think):
const users = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }];
const addUser = (username) => {
const user = users.find((user) => user.username === username);
if (user) return { ...user, new: false };
const newUser = {
id: users.length + 1,
username,
};
users.push(newUser);
return { ...newUser, new: true };
};
Here is how that might look as a live example:
const users = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }];
const addUser = (username) => {
const user = users.find((user) => user.username === username);
if (user) return { ...user, new: false };
const newUser = {
id: users.length + 1,
username,
};
users.push(newUser);
return { ...newUser, new: true };
};
// a little extra scripting here to support the input and button in the example
const form = document.querySelector('form');
const input = document.querySelector('input');
const span = document.querySelector('span');
const pre = document.querySelector('pre');
const syncDataWithPre = () => {
pre.innerHTML = JSON.stringify(users, null, 2);
};
form.onsubmit = (e) => {
e.preventDefault();
span.textContent = '';
if (input.value) {
const user = addUser(input.value);
const { new: isNew, ...userDetails } = user;
span.classList[isNew ? 'add' : 'remove']('new');
span.textContent = `User ${isNew ? 'added' : 'already exists'}`;
}
input.value = '';
syncDataWithPre();
};
syncDataWithPre();
body {
font-family: arial, sans-serif;
}
span {
display: block;
padding-top: 8px;
font-weight: 700;
color: #777;
}
span:empty {
display: none;
}
.new {
color: #0a0;
}
.existing: {
color: #777;
}
<form>
<input placeholder="New username" />
<button>Add user</button>
</form>
<span></span>
<pre></pre>
Native functions of array are sometimes 3X - 5X times slower than normal loops. Plus native functions wont work in all the browsers so there is a compatibility issues.
My Code:
<script>
var obj = [];
function checkName(name) {
// declarations
var flag = 0;
var len = obj.length;
var i = 0;
var id = 1;
// looping array
for (i; i < len; i++) {
// if name matches
if (name == obj[i]['username']) {
flag = 1;
break;
} else {
// increment the id by 1
id = id + 1;
}
}
// if flag = 1 then name exits else push in array
if (flag == 0) {
// new entry push in array
obj.push({'id':id, 'username': name});
}
}
// function end
checkName('abc');
</script>
This way you can achieve result faster.
Note: I have not checked if parameter passed is empty or not, if you want you can put a check on it or write a regular expression for particular validation.
xorWith in Lodash can be used to achieve this
let objects = [ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]
let existingObject = { id: 1, username: 'fred' };
let newObject = { id: 1729, username: 'Ramanujan' }
_.xorWith(objects, [existingObject], _.isEqual)
// returns [ { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]
_.xorWith(objects, [newObject], _.isEqual)
// returns [ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ,{ id: 1729, username: 'Ramanujan' } ]
Check it here :
https://stackoverflow.com/a/53644664/1084987
You can create something like if condition afterwards, like
if(!contains(array, obj)) add();
You can try this also
const addUser = (name) => {
if (arr.filter(a => a.name == name).length <= 0)
arr.push({
id: arr.length + 1,
name: name
})
}
addUser('Fred')
function number_present_or_not() {
var arr = [2, 5, 9, 67, 78, 8, 454, 4, 6, 79, 64, 688];
var found = 6;
var found_two;
for (i = 0; i < arr.length; i++) {
if (found == arr[i]) {
found_two = arr[i];
break;
}
}
if (found_two == found) {
console.log('number present in the array');
} else {
console.log('number not present in the array');
}
}
I was given a condition to check the data in the table of the mysql database the object array of my table consist of the id, latitude and longitude as column names, I have to check whether the location is in the database else insert this into the table so:
I created a function of handle submit called by a button,
handle Submit = (event) => {
const latitude = document.getElementById("latitude").innerHTML;
const longitude = document.getElementById("longitude").innerHTML;
const found = this.state.data.some((el) => el.latitude === latitude);
if (!found) {
Axios.post("http://localhost:3001/api/insert", {
latitude: latitude,
longitude: longitude,
}).then(() => {
alert("successful insert");
});
console.log(latitude, longitude);
}
};
Here you can see the conditional statement for inserting if not exist in the database.
Please have a look on given example
$(document).ready(function(){
const arr = document.querySelector(".list");
var abcde = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' }];
$("#btnCheckUser").click(function() {
var tbUsername = $("#tbUsername").val();
if (abcde.some(obj => obj.username === tbUsername)) {
alert('existing user ' + tbUsername);
return;
}
else {
abcde.push({ id: abcde.length + 1, username: tbUsername });
alert('added new user ' + tbUsername);
arr.appendChild(createArray(tbUsername));
return;
}
});
function createArray(name) {
let li = document.createElement("li");
li.textContent = name;
return li;
}
abcde.forEach((x) => arr.appendChild(createArray(x.username)));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<p>Add text and click on Check.</p>
<input type="text" id="tbUsername" />
<button type="button" id="btnCheckUser">Check</button>
<div class="list">
<ul></ul>
</div>
const __checkIfElementExists__ = __itemFromArray__ => __itemFromArray__.*sameKey* === __outsideObject__.*samekey*;
if (cartArray.some(checkIfElementExists)) {
console.log('already exists');
} else {
alert('does not exists here')