Check and get data from an array in JavaScript - javascript

I have an object:
var Obj1 = {id: 1, name: 'Apple'}
And an array object:
var ArrObj = [ {id: 1, name: 'Apple', 'eat': 'rice}, {'id: 2', 'name': 'Banana'}]
How do I check Obj1.id in ArrObj? And I want the result to be: { id:1, name: 'Apple', 'eat':'rice'}

You can use Array.find():
var Obj1 = {id: 2, name: 'Banana'}
var ArrObj = [ {id: 1, name: 'Apple', 'eat': 'rice'}, {'id': 2, 'name': 'Banana'}];
var res = ArrObj.find(({id}) => id === Obj1.id );
console.log(res);
You can also use array destructuring way like:
var Obj1 = {id: 2, name: 'Banana'}
var ArrObj = [ {id: 1, name: 'Apple', 'eat': 'rice'}, {'id': 2, 'name': 'Banana'}];
var res = ArrObj.find(({id}) => id === Obj1.id);
console.log(res);

You could also use the filter function like this:
let result = ArrObj.filter(obj => {
return obj.id == Obj1.id
})
Documentation is here: Array.prototype.filter()

all right!
you can also add array and get it by code :
var obj = '{ "name" : "amr" , "age" : "16"}';
var obj1 = JSON.parse(obj);
alert("yourname is : "+obj1.name+" , your age is "+obj1.age);
// it get name > amr and age > 16
it's very easy :)

Related

Delete multiple objects in an array by id

I have a main array of objects with each object having some key/values as well as a "id" key with 1,2,3,4,5, etc
Now I have another array representing just id's (like [2,3])
I want to use this array to delete objects from the main array...so in this case, objects from the main array having id's 2 & 3 should be deleted
While I am aware of findBy(id), I am not sure if that can be used to delete multiple objects at once.
You can use filter. In the filter callback function check if the id is also there in id array by using includes
let idArr = [1, 2]
let obj = [{
id: 1,
name: 'abc'
},
{
id: 2,
name: 'abc'
},
{
id: 3,
name: 'abc'
},
{
id: 4,
name: 'abc'
}
];
let data = obj.filter(item => !idArr.includes(item.id));
console.log(data);
console.log(obj)
using filter might work well here. you could write something like:
var newArray = oldArray.filter(object => !ids.includes(object.id))
You can do it, like this:
[2,3].forEach(key => {
delete object[key];
})
You can use filter method for this.
Ex:
let id = 2;
let list = [{
Id: 1,
Name: 'a'
}, {
Id: 2,
Name: 'b'
}, {
Id: 3,
Name: 'c'
}];
let lists = list.filter(x => {
return x.Id != id;
})
console.log(lists);
Assuming you want to delete items from the original array by entirely removing the element from the array (and you don't want to get a new array), you can take advantage of
Array.splice
let idArr = [1, 2];
let obj = [{
id: 1
},
{
id: 2
},
{
id: 3
},
{
id: 4
}
];
for (let id of idArr) {
// look for the element by its id.
const objIdRef = obj.find(i => i.id === id);
// if it actually exists, splice it.
objIdRef && obj.splice(obj.indexOf(objIdRef), 1);
}
console.log(obj);
If the obj array is big, you might want to make a map from it before processing the id array, so that the complexing is reduced to O(1) when the delete process begins.
Perhaps This is what you want:
var arr= [{id:1, name: "foo"}, {id:2, name: "bar"}, {id:3, name:"not to be deleted"}];
var idsToDelete = [1, 2];
var res = arr.map((i, idx)=>{
return arr[idx] = idsToDelete.includes(i.id)? undefined : arr[idx]
}).filter(i=>i)
console.log(res)
You can try Lodash.js functions _.forEach() and _.remove()
let valuesArr = [
{id: 1, name: "dog"},
{id: 2, name: "cat"},
{id: 3, name: "rat"},
{id: 4, name: "bat"},
{id: 5, name: "pig"},
];
let removeValFromIndex = [
{id: 2, name: "cat"},
{id: 5, name: "pig"},
];
_.forEach(removeValFromIndex, (indi) => {
_.remove(valuesArr, (item) => {
return item.id === indi.id;
});
})
console.log(valuesArr)
/*[
{id: 1, name: "dog"},
{id: 3, name: "rat"},
{id: 4, name: "bat"},
]; */
Don't forget to clone (_.clone(valuesArr) or [...valuesArr]) before mutate your array

Remove an array element if one of the property contains certain values

I have following variable:
var data = [{id: '1', name: 'demo1'}, {id: '2', name: 'demo2'}, {id: '3', name: 'demo3'}]
Now I have another list of ids,
var lookFor = ["2", "3"];
Now, from data how will I remove the objects with id not available in the lookFor array of ids.
I tried with following
_.filter(data, function(item) {
return _.contains(lookFor, 'id');
});
Is there any other way?
Uhm consider the following?
var data = [{id: '1', name: 'demo1'}, {id: '2', name: 'demo2'}, {id: '3', name: 'demo3'}];
var keys = ["2", "3"];
let filteredArray = data.filter(element => keys.indexOf(element.id) != -1);
console.log(filteredArray);
// Output is now
// 0: {id: "2", name: "demo2"}
// 1: {id: "3", name: "demo3"}
Use this the get the opposite:
let filteredArray = data.filter(element => keys.indexOf(element.id) == -1);
console.log(filteredArray);
// Output is:
// 0: {id: "1", name: "demo1"}
Is that what you want?
Have a nice day, Elias
You can use functor filter for array and method includes to
determine whether an array includes a certain element
var res = data.filter(el=>!lookFor.includes(el.id));
result
[{id: "1", name: "demo1"}]
Opposite
var res = data.filter(el=>lookFor.includes(el.id));
You could use _.remove(array, [predicate=_.identity]) as following. Notice that this mutates the value of data.
var data = [{id: '1', name: 'demo1'}, {id: '2', name: 'demo2'}, {id: '3', name: 'demo3'}];
var lookFor = ["2", "3"];
_.remove(data, function(n) {
return _.indexOf(lookFor, n.id) === -1;
});
console.log(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>
Here are just a few examples of how you can achieve this with ES6 only or with lodash. There are obviously more ways to do it but those should give you an idea. Also note that most of the examples do not mutate the arrays utilized:
var data = [{id: '1', name: 'demo1'}, {id: '2', name: 'demo2'}, {id: '3', name: 'demo3'}]
var lookFor = ["2", "3"];
// Does not mutate the array and uses ES6 Filter
var withFilter = data.filter(x => lookFor.indexOf(x.id) >= 0)
// Does not mutate the array and uses ES6 Reduce
var withReduce = lookFor.reduce((r,c) => r.push(data.find(x => x.id === c) || []) && r,[])
// Does not mutate the array with Lodah Filter
var withLodashFilter = _.filter(data, x => _.includes(lookFor, x.id))
// Mutates the array with Lodash Remove
var withLodashRemove = _.remove(data, x => _.includes(lookFor, x.id))
console.log('ES6 Filter', withReduce)
console.log('ES6 Reduce', withFilter)
console.log('Lodah Filter', withLodashFilter)
console.log('Lodash Remove', withLodashRemove)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

How to get data by id from an object

I have an object Like
How to get single row by Id?
You can use Array#find function and pass a condition into it like
arr.find(item => item.id === 1)
Example
const users = [
{id: 1, name: 'A'},
{id: 2, name: 'B'},
];
const user = users.find(item => item.id === 1);
console.log(user);
Use find()
var item = yourArray.find(item => item.id === 2053);
DEMO
const yourArray = [
{Id: 2053, title: 'sass'},
{Id: 2054, title: 'sdss'},
];
const found = yourArray.find(item => item.Id ===2053);
console.log(found);
Yo can also use
var requiredItem = array.filter(i => i.id == 2054)
Example
var arrayNew = [
{id: 2053, name: 'sxsxs'},
{id: 2054, name: 'sss'}
];
var requiredItem = arrayNew.filter(i => i.id == 2054);
console.log(requiredItem);
let obj = [{
"id" : 1,
"Title" : "Hi"
},{
"id" : 11,
"Title" : "Hello"
}]
function filterById(ids) {
return obj.filter((obj) => {return obj.id == ids})
}
console.log(filterById(11))

Javascript merge arrays when one contains updated data

I have two arrays of objects like this:
var myArray = [
{pk: '1', person: 'person 1'},
{pk: '2', person: 'someone'},
];
var updatedArray = [
{pk: '2', person: 'another person'}
];
I'm looking to merge the two in the most efficient way possible.
My thought was to merge the older myArray into the newer updatedArray and leave out any items where myArray.pk == updatedArray.pk
I'm having trouble trying to get it going with either jQuery or underscore.
I've been trying to use this one as an example as well.
Any ideas?
I think looping through updates and copy it over to current value would be a good way.
If you have just one property to change then there is no need for Object.assign. you can simply replace it with
myArray[idx].name = uv.name
var myArray = [
{pk: '1', person: 'person 1'},
{pk: '2', person: 'someone'},
];
var updatedArray = [
{pk: '2', person: 'another person'}
];
updatedArray.forEach(uv => {
var idx = myArray.findIndex(v => v.pk === uv.pk)
if (idx != -1)
myArray[idx] = Object.assign({}, myArray[idx], uv);
else myArray.push(uv);
});
console.log(myArray)
You can try
function mergeArr(arrOne, arrTwo, prop) {
_.each(arrTwo, function(arrTwoobj) {
var arrOneobj = _.find(arrOne, function(arrOneobj) {
return arrOneobj[prop] === arrTwoobj[prop];
});
arrOneobj ? _.extend(arrOneobj, arrTwoobj) : arrOne.push(arrTwoobj);
});
}
var myArray = [
{pk: '1', person: 'person 1'},
{pk: '2', person: 'someone'},
];
var updatedArray = [
{pk: '2', person: 'another person'}
];
mergeArr(myArray, updatedArray, 'pk');
console.log(myArray);
var myArray = [
{pk: '1', person: 'person 1'},
{pk: '2', person: 'someone'},
];
var updatedArray = [
{pk: '2', person: 'another person'}
];
var exists = [];
for (item of updatedArray) {
exists[item.pk] = true;
}
for (item of myArray) {
if (!exists.hasOwnProperty(item.pk)) {
updatedArray.push(item);
}
}
console.log(updatedArray);
Most efficient way would be, first create a map from one of the arrays.
Then run one loop and get the items from the map
you need just two functions, reduce and map
This way only need array1.length + array2.length iterations, not array1.length * array2.length
// creates the map
var myMap = myArray.reduce(function(obj, item) {
obj[item.pk] = item
}, {})
// merges updatedArray items, with the item from the map
var mergedArray = updatedArray.map(function(item) {
return myMap[item.pk] ? Object.assign({}, myMap[item.pk], item) : item
})

Javascript: Convert a list of objects to an associative array

I'm looking for a way to take an array of JavaScript objects and get an associative array of those objects keyed by some attribute.
For example, given this array of objects:
var data = [
{'id': 100, name: 'bob', foo: 'bar'},
{'id': 200, name: 'john', foo: 'qux'}
];
I want to be able to look up each object by its id, so I want an object like this:
var new_data = {
100: {name: 'bob', foo: 'bar'},
200: {name: 'john', foo: 'qux'}
}
// now I can use new_data[200] to access `john`
While I'm sure it's easy enough to construct a new object and then iterate over each object in the original array and append a new key:value pair to new object, I was wondering if there was a more concise way of doing it.
In ES6:
Object.assign({}, ...data.map(({id, name, foo}) => ({[id]: {name, foo}})))
This maps each object in the input into a single-property object with the id as key, then spreads those into parameters to Object.assign which will glue them together for you.
Or,
construct a new object and then iterate over each object in the original array and append a new key:value pair to new object
You can do essentially what you just said but in relatively concise form using reduce:
data.reduce((result, {id, name, foo}) => {
result[id] = {name, foo};
return result;
}, {})
You may try this:
var data = [
{'id': 100, name: 'bob', foo: 'bar'},
{'id': 200, name: 'john', foo: 'qux'}
];
data.reduce(function(p, c){
p[c.id] = {name:c.name, foo: c.foo};
return p;
}, {});
Here is a solution using Array maps:
var data = [{
'id': 100,
name: 'bob',
foo: 'bar'
}, {
'id': 200,
name: 'john',
foo: 'qux'
}];
var new_data = {};
// Iterate over data
data.map(obj => {
// Create new object from old
new_data[obj.id] = {
'name': obj.name,
'foo': obj.foo
}
});
console.log(new_data);
In ES5:
working JSBIN: https://jsbin.com/qudeze/edit?js,console
var data = [
{'id': 100, name: 'bob', foo: 'bar'},
{'id': 200, name: 'john', foo: 'qux'}
];
var new_data = {
100: {name: 'bob', foo: 'bar'},
200: {name: 'john', foo: 'qux'}
};
var y = data.reduce(function(result, next) {
result[next.id] = {name: next.name, foo: next.foo};
return result;
}, {});
console.log(y);
Your code should be go like this....
<script type="text/javascript">
var data = [
{'id': 100, name: 'bob', foo: 'bar'},
{'id': 200, name: 'john', foo: 'qux'}
];
var new_data = {};
for(var i=0; i<data.length; i++){
var element = {};
element["name"] = data[i].name;
element["foo"] = data[i].foo;
new_data[data[i].id] = element;
}
console.log(JSON.stringify(new_data));
I don't think we could use map method. Because our output is not an array but an object. We could use each method to help us. Here is a sample code:
var data = [
{'id': 100, name: 'bob', foo: 'bar'},
{'id': 200, name: 'john', foo: 'qux'}
];
var result = {};
data.forEach(function(item){
var key = item.id;
//remove id from item
delete item.id;
result[key] = item;
});
console.log(result);
Note, this solution will modify the original array. If you don't want change the original one, just copy one.

Categories

Resources