Javascript array of objects check for key [duplicate] - javascript

This question already has answers here:
How do I find an array item with TypeScript? (a modern, easier way)
(5 answers)
Closed 5 years ago.
So my question how can I check this array of objects by "key" for eg I want to check if the key 3892 is there, I have tried with indexOf but no luck, I can't use a loop.

You can use some() and hasOwnProperty()
var array = [{3892: 'value'}, {1234: 'value'}];
var check = array.some(obj => obj.hasOwnProperty(3892));
console.log(check)

You can chain Object.keys with Array.prototype.includes to achieve that
Object.keys(myObject).includes(myKey);
const myObject = { name: 'Peter' };
const myKey = 'name';
const result = Object.keys(myObject).includes(myKey);
console.log(`Includes key ${myKey}? `, result);

Related

How to compare two different Object keys and update it's value if keys are same in Javascript? [duplicate]

This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 2 years ago.
Here i am having two different objects :
let oldObject = {name:"Dhanush",age:24,sex:"Male",education:"Btech"}
let newObject = {name:"Dhanush kumar S",age:23,sex:"Male"}
result should be comparing this above two objects and check if the key are same , then update the oldObject value with newObject . The result has to be like this
let updatedObject = {name:"Dhanush kumar S",age:23,sex:"Male",education:"Btech"}
I tried by doing something like this, but this doesnt help. Your help is much appreciated.
const compareObjects = () => {
for (let [key,value] in oldObject) {
if (newObject.hasOwnProperty(key)) {
oldObject[newObject[key]] = newObject[value]
delete oldObject[key]; //remove old entry
}
}
console.log(oldObject)
}
compareObjects()
You can do this by using the spread syntax.
Just spread the old object first followed by the new object.
Matching keys if any would be updated by the values from the new object and new keys in the new object would be added:
let oldObject = {name:"Dhanush",age:24,sex:"Male",education:"Btech"}
let newObject = {name:"Dhanush kumar S",age:23,sex:"Male"}
const merge = (oldObj, newObj) => {
return {...oldObj, ...newObj};
}
console.log(merge(oldObject, newObject));

Implicit return with spread operator in .map() function [duplicate]

This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 3 years ago.
How to use spread operator in implicit return wherein I'm removing one key value from the array of the object. I can use explicit return but I want shortcode and other possible solution of the scenario.
let array = [
{"sales":2341,"targetMet":false,"advertisment":true},
{"sales":981,"advertisment":true},
{"sales":3423,"targetMet":true,"advertisment":false},
{..},
{..}
];
let expectedArray = array.map(({targetMet,...rest}) => {...rest});
console.log(expectedArray) // should remove all targetMet keys
Just return rest no need to spread it again.
Note: To return an object from arrow function wrap object in (). But here you don't need that.
let array = [
{"sales":2341,"targetMet":false,"advertisment":true},
{"sales":981,"advertisment":true},
{"sales":3423,"targetMet":true,"advertisment":false},
];
let expectedArray = array.map(({targetMet,...rest}) => rest);
console.log(expectedArray)

filter an array of objects based on matching key/value property? [duplicate]

This question already has answers here:
javascript filter array of objects
(8 answers)
Closed 4 years ago.
This is the issue Im trying to solve. I know how to do direct comparison with objects, but Im caught how to make an either or property comparison inside a filter fx.
// tasks are objs with {name: string,id: int}
const taskArray = [task1, task2, task3, task3];
// f(x) needs to take obj param and see if it matches either/or with
// any task obj k/v pair
// my implementation so far, missing something important, help here
const filterTasks = (taskArray, obj) => {
taskArray.filter( task => Object.keys(task) // i know I have to do some equality opperator here but stuck
return taskArray;
}
There it is.
filterTasks will return an array that contains only tasks in the passed array that match at least 1 key/val pair in the passed object.
tArr = [{name: "hi",id: 1},{name: "hola",id: 2},{name: "hello",id: 3},{name: "bye",id: 4}];
const filterTasks = (taskArray, obj) => taskArray.filter( task => Object.keys(task).some( key => obj[key] && obj[key]==task[key]));
console.log(filterTasks(tArr,{name:"hi",id:2}));

Get single object from array using JavaScript functions [duplicate]

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 6 years ago.
I have this array of objects:
var frequencies = [{id:124,name:'qqq'},
{id:589,name:'www'},
{id:45,name:'eee'},
{id:567,name:'rrr'}];
I need to get an object from the array above by the id value.
For example I need to get the object with id = 45.
I tried this:
var t = frequencies.map(function (obj) { return obj.id==45; });
But I didn't get the desired object.
How can I implement it using JavaScript prototype functions?
If your id's are unique you can use find()
var frequencies = [{"id":124,"name":"qqq"},{"id":589,"name":"www"},{"id":45,"name":"eee"},{"id":567,"name":"rrr"}];
var result = frequencies.find(function(e) {
return e.id == 45;
});
console.log(result)
You need filter() not map()
var t = frequencies.filter(function (obj) {
return obj.id==45;
})[0];

Any better way to make a object into another object in javascript [duplicate]

This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 9 years ago.
Oldobject = {'name':'Joe','age':12};
newObject = {'address':'XXX'};
I want to get result like :
{'name':'Joe','age':12,'address':'XXX' }
I use
Oldobject.address = newObject
Is there any graceful way to do this ?
for (var property in Oldobject) {
newObject[property] = Oldobject[property];
}
Use a for... in loop, like:
for( var property in newObject ){
OldObject[property] = newObject[property]
}
This will put all of the stuff from newObject into oldObject.

Categories