I got a Javascript object which looks like this:
{
key1: {
itemId: "someId",
myValue: "value"
},
key2: {
itemId: "someId2",
myValue: "value2"
}
}
I also got an array to which I push items like this:
myArr.push({[item.itemId] : anotherDefinedObject}); //inside a loop on items
I then want to join all the items I pushed to the array to the first object. Meaning that if for example the items ids were "key3" and "key4" I would get this object:
{
key1: {
itemId: "someId",
myValue: "value"
},
key2: {
itemId: "someId2",
myValue: "value2"
},
key3: { //the object that was added },
key4: { //the object that was added }
}
I tried doing it with the spread operator:
return {...object, ...myArr}
But then instead of getting what I needed, I get "0", "1" and such as the keys in the new object (since 0,1 etc are the keys of the arrays).
How do I concatenate the object and the array the way I want?
rather my myArray create myObject.
myObject = {}
myObject[item.itemId] = anotherDefinedObject;
Then
return {...object, ...myObject}
const obj = {key1: 1};
const myArr = [{key2: 2}];
myArr.forEach(val => Object.assign(obj, val));
console.log(obj); // {key1: 1, key2: 2}
What you're looking to achieve here is not possible with the spread operator; it's working exactly as intended and the functionality cannot be changed as JS doesn't allow operator overloading. Your best bet is to loop through myArr and add each item in the array to the object using your own code.
P.S. As the comments suggest, in the future you should really provide more examples of your input, the current output, and your intended output; it was quite difficult to follow without it.
Related
My usage will contain 6 different object types (some which contain double nested arrays), and any possibility of number of entries, on the condition that an given entry is unique.
These objects do not have a consistent unique identifier (a unique identifier is applied in backend on submission).
here is an example of what the array may look like (only 2 object types):
arr = [
{name:"aaa",time:15},
{name:"aaa",time:22},
{timeline: "250", chars[{a},{b},{c}]},
{timeline: "220", chars[{d},{e},{f}]},
]
obj = {name:"aaa",time:22}
My intention is to gain a true or false based on if obj is inside arr
I have tried methods:
I was suggested this method & it errors: #<Object> is not a function
console.log(arr.find(obj))
I also found this suggestion but it will always return false even with the element present
console.log(arr.includes(object))
I tried this method myself, though it will always fail.
console.log(arr.filter((element, index) => element === obj)
With attempt 4, If I was to compare name, this would be insufficient as unique time would be ignored missing valid entries.
If I was to pass every field, this would also not work as each object may or may not have the field and cause error.
Its not really possible to manually pre-filter filter into distinct categories, as every time a new type is added it will need manually adding to the filter.
If there is a library which could do this that you know of, please let me know as that would be perfect. Otherwise any other suggestions (excluding separating arrays) Would be greatly appreciated.
Use arr.some() to check if the required object is present in the array.
To compare the objects, a simpler way is to Stringify both the Objects and compare them.
const arr = [
{name:"aaa",time:15},
{name:"aaa",time:22},
{name: "aaa", chars: ["a", "b", "c"]},
{name: "bbb", chars: ["d", "e", "f"]},
]
const obj1 = {name:"aaa", time: 15}
const obj2 = {name:"aaa",chars: ["a", "b", "c"]}
console.log(arr.some((element) => JSON.stringify(element) === JSON.stringify(obj1))) // true
console.log(arr.some((element) => JSON.stringify(element) === JSON.stringify(obj2))) // true
Didn't give much thought on performance.
I didn't put much thought on performace here but this might help:
function checkObjectInArray(arr, obj) {
const res = arr.some((el) => deepEqual(el, obj));
console.log(res);
}
function deepEqual(obj1, obj2) {
if (Object.keys(obj1).length !== Object.keys(obj2).length) return false;
for (let prop in obj1) {
if (!obj2.hasOwnProperty(prop) || obj2[prop] !== obj1[prop]) {
return false;
}
}
return true;
}
in your case you can use it like:
arr = [
{ name: "aaa", time: 15 },
{ name: "aaa", time: 22 },
{ timeline: "250", data: ["2", "3", "4"] },
{ timeline: "251", data: ["2", "3", "4"] }, // what is chars[{d},{e},{f}] ?!
];
obj = { name: "aaa", time: 22 };
checkObjectInArray(arr, obj);
Observation : arr is not a valid array. Nested chars is not containing a valid value.
Solution : You can simply achieve the requirement by Just converting the JSON object into a JSON string and by comparing.
This solution works fine as you are just trying to find a single object in the passed arr.
Live Demo :
const arr = [
{name:"aaa",time:15},
{name:"aaa",time:22},
{timeline: "250", chars: [{a: 1},{b: 2},{c: 3}]},
{timeline: "220", chars: [{d: 4},{e: 5},{f: 6}]},
];
const obj = {name:"aaa",time:22};
const res = JSON.stringify(arr).indexOf(JSON.stringify(obj)) !== -1 ? true : false;
console.log(res);
I have an object array with object key value.
arr = [obj1:{name:"Jack", surname:"Peralto"}, obj2:{name:"Husnu", surname:"White"}]
I do not want to see obj1 and obj2 labels. Because of these labels I could not use a word template package.
So I want to convert it to this form.
arr = [{name:"Jack", surname:"Peralto"}, {name:"Husnu", surname:"White"}]
.map function does not work at first array.
arr.map(o=>o)
Why I have an array like this? I should use reduce function and obj1 and obj2 labels are key value when I create object array. Now I don't need them.
When you fix the syntax errors in your code ([obj1:{... is not a valid data structure) you can use Object.values to get at the nested objects.
const obj = {
obj1: {
name: "Jack",
surname: "Peralto"
},
obj2: {
name: "Husnu",
surname: "White"
}
};
console.log(Object.values(obj));
its not the right syntax of array you have used array don't have key value pair
use this syntax if you are using object of objects with key value pair
const obj = {
obj1: {
name: "Jack",
surname: "Peralto"
},
obj2: {
name: "Husnu",
surname: "White"
}
};
or use this an array contains object
arr = [
{name:"Jack", surname:"Peralto"},
{name:"Husnu", surname:"White"}
]
Title. I can have two or three types of objects, but i would like to make reliable function to cover all other cases if any new keys/properties will be added to future objects.
Whenever i'm mapping through objects with the same properties (obj1, obj4) everything is K. The problem is when i want to make a function to map through all objects in arr with keys that may or may not be in particular object.
I was thinking about if ('key' in obj) but that wouldn't make function reliable for future, if new keys might be added unless I could analyze all object keys in arr, by creating new array with unique key objects. But if object will have 10 keys and there will be 1000+ objects in an array I assume there will be performance issues; correct me if i'm wrong.
Same goes for .hasOwnProperty().
const arr = [
obj1 = {
key1: val1,
key2: val2,
key3: val3
},
obj2 = {
key3: val3
},
obj3 = {
key1: val4,
key2: val5,
key3: val1
},
obj4 = {
key1: val6,
key2: val7
}
]
Instead of gussing the keys that an object could have, you can use Object.keys or a for...in loop to dynamically get or loop through keys of an object. Like this:
var obj = {
"key1": 2,
"key2": 3,
"key-unknown": 5
}
console.log("METHOD 1:");
var keys = Object.keys(obj);
console.log("obj has these keys: ", keys);
console.log("\nMETHOD 2:");
for(var key in obj) {
console.log("'" + key + "' is a key");
}
Using the above two methods you can safely and dynamically use only the keys that the object has and not use a set of predefined static keys that may or may not be in the object and that could be fewer than what the object actually has.
From a wild guess you may be in need of a function such as;
function fillMissingKeys(a){
var keys = a.reduce((k,o) => Object.assign(k,Object.keys(o)),[])
.reduce((o,k) => (o[k] = null,o),{});
return a.map(o => Object.assign({},keys,o));
}
var arr = [
obj1 = {
key1: "val1",
key2: "val2",
key3: "val3"
},
obj2 = {
key3: "val3"
},
obj3 = {
key1: "val4",
key2: "val5",
key3: "val1",
key4: "val8"
},
obj4 = {
key1: "val6",
key2: "val7"
}
];
console.log(fillMissingKeys(arr))
I have an object that looks something like this
{id: "2", name: "foo", price: "1"}
I want to transform this to the following
{2: {id: "2", name: "foo", price: "1"}}
I'm able to achieve this today if I wrap this object with a simple array like so thanks to the friendly keyBy method for array
_.keyBy([action.response], function(item) { return item.id; });
What I would prefer of course is the same result but without having to wrap this with an array first. Does transform/reduce or some other lodash v4 method provide this functionality?
Short solution based on ES6 computed property names: { [obj.id]: obj }
Example:
var obj = {id: "2", name: "foo", price: "1"}
var transformed = { [obj.id]: obj }
console.log(transformed)
You can do this directly with a function:
function convert(obj) {
var result = {}
result[obj.id] = obj
return result
}
Is that what you are looking for?
I have an array of JSON-like object[Object,Object...] (I was calling it array of JSON object until I see this ) and I would like to go through each item in it. The JSON object has 3 values {name:"name",date:"2015-01-01",c:3} in it and I want to put each of them into a new array. Should I use a for item in JSONArary{item = ...} or for (i=0,i<len,i++){array[i] = ....} or should I JSONArray.pop()? Which one is faster and why? What if I want to reverse the array? Do reversing cost a lot?
for (i=0,i<len,i++){array[i] = ....} should be faster than for item in JSONArary{item = ...} because the later will traverse all enumerable properties on this object, while some of these properties are unnecessary.
When you want to iterate over just the indexed properties of an array, the only guaranteed way to keep things semantically consistent is to use an integer index.
For your reference: Why is using "for...in" with array iteration a bad idea?
Even Google make the JavaScript coding style guide as:
for-in loop: Only for iterating over keys in an object/map/hash
I have a faster way, use $.map:
var oldArray = [{
name: "name1",
date: "2015-01-01",
c: 3
}, {
name: "name2",
date: "2015-01-01",
c: 3
}, {
name: "name3",
date: "2015-01-01",
c: 3
}];
var newArray = $.map(oldArray, function (item) {
return {
FullName: item.name,
RegisterDate: item.date
};
});
alert(JSON.stringify(newArray));
Hope this help.