Get matching values of two objects - javascript

I have an object inboundTransfer which contains an unknown number of keys. I would like to get all values from the itemNumbersList properties which match the values on an array Object.keys(selectedItems). The data that I receive looks a little something like this:
const selectedItems = {
'nameX': {
propert1: 'value1',
property2: 'value2,
...
}
},
'nameY': {
...
},
'nameT': {
...
}
const inboundTransfers = {
0: {
property1: 'value1',
itemNumbersList: ['nameX', 'nameY', 'nameZ'],
},
1: {
property2: 'value2',
itemNumbersList: ['nameK', 'nameJ', 'nameT']
},
...
}
const isOnTransferlist = Object.keys(inboundTransfers)
.map((transfer) => Object.values(inboundTransfers[transfer].itemNumbersList)
.some((item) => Object.keys(selectedItems)
.indexOf(item) >= 0));
Obviously, I currently only check if the value is on both lists or not. Ideally, I would like to get the value itself. How could I achieve that? I tried using .filter instead of map but with no success.
Any help is much appreciated!

I implemented this function and I test it, it works well.
function match() {
const selectedItems = {
a: "a1",
b: "b1",
};
const inboundTransfers = {
0: {
items: ["a", "b"],
},
1: {
items: ["c", "b"],
},
};
const capturedValues = []; // founded items store
[...Object.keys(selectedItems)].forEach((arrKey) => {
for (let [key, items] of Object.entries(inboundTransfers)) {
Object.values(items).forEach((item) => {
item.forEach((i) => {
if (i === arrKey) {
capturedValues.push(i);
}
});
});
}
});
// Remove redundant values
console.log([...new Set(capturedValues)]);
}

I found a way that works, but it's not very elegant, I guess:
const list = [];
Object.keys(inboundTransfers)
.forEach((transfer) => {
Object.values(inboundTransfers[transfer].itemNumbersList)
.forEach((item) => {
if (Object.keys(selectedItems).includes(item)) {
list.push(item);
}
});
});

Related

How to convert an array of objects into a single object using reduce?

I have the following array of object.
[
{ claimNumber1: 'R12345', checkNumber1: '' },
{ claimNumber2: 'T1234', checkNumber2: 'abcd' },
{ claimNumber3: 'Z4567', checkNumber3: 'qwer' }
]
Using reduce, I want to convert this to below.
{
claimNumber1:'R12345',
checkNumber1:'',
claimNumber2:'T1234',
checkNumber2:'',
claimNumber3:'Z4567',
checkNumber3:'',
}
I tried below but didn't get what I expected.
.reduce((obj, item) =>{
return {...obj,item}
} ,{});
You should spread the item object, because item is an object
const arr = [
{ claimNumber1: "R12345", checkNumber1: "" },
{ claimNumber2: "T1234", checkNumber2: "abcd" },
{ claimNumber3: "Z4567", checkNumber3: "qwer" },
];
const result = arr.reduce((obj, item, i) => {
return { ...obj, ...item, [`checkNumber${i + 1}`]: "" };
}, {});
console.log(result);
Almost there. You just need to spread the item as well.
.reduce((obj, item) => {
return {
...obj,
...item,
};
}, {});
I think you should spread every item in reducer.
Here is my code.
const res = arr.reduce((prev, item) => {
return { ...prev, ...item };
}, {});
And result is
{
claimNumber1: 'R12345',
checkNumber1: '',
claimNumber2: 'T1234',
checkNumber2: 'abcd',
claimNumber3: 'Z4567',
checkNumber3: 'qwer'
}
I'm not sure of the benefit of using reduce in this situation. A simple loop would be self-documenting, and easier to read.
const data = [
{ claimNumber1: 'R12345', checkNumber1: '' },
{ claimNumber2: 'T1234', checkNumber2: 'abcd' },
{ claimNumber3: 'Z4567', checkNumber3: 'qwer' }
];
const out = {};
// For every object in the array
for (const obj of data) {
// Get an array of its keys and iterate over them
for (const key of Object.keys(obj)) {
// If it's a claim add that value to the output
// object property, otherwise set that property value
// to an empty string.
if (key.startsWith('claim')) {
out[key] = obj[key];
} else {
out[key] = '';
}
}
}
console.log(out);
Additional documentation
Object.keys

how to get array according to conditions in javascript

My array comes like this
var data=[{PRODUCT : P1}, {PRODUCT: P2}]
I wantt to convert this into [P1, P2].
Sometimes my array comes like this
var data=[{ITEM: I1, QUANTITY:1}, {ITEM: I2, QUANTITY:2}]
I wantt to convert this into [I1, I2].
so can we make a common function, where I just want to extract particular value of array and make a new array.
p.s. Thank you in advance
I tried to write the logic like this:
data.map((d, index) => { var result= [];
result.includes(d[0]); })
but it,s not dynamic
You could define a function which will always get the first value of the first object key, this should satisfy your needs based on the above
var data1 = [{
ITEM: 'I1',
QUANTITY: 1
}, {
ITEM: 'I2',
QUANTITY: 2
}]
var data2 = [{
PRODUCT: 'P1'
}, {
PRODUCT: ' P2'
}]
function getArrayOfValues(list) {
return list.reduce((acc, x) => {
const firstValue = Object.values(x)[0];
acc.push(firstValue)
return acc;
}, [])
}
const result1 = getArrayOfValues(data1)
console.log(result1)
const result2 = getArrayOfValues(data2)
console.log(result2)
function getProductOrItem(list) {
return list.reduce((accumulator, obj) => {
if (obj.PRODUCT) {
accumulator.push(obj.PRODUCT);
} else if (obj.ITEM) {
accumulator.push(obj.ITEM);
}
return accumulator;
}, [])
}
you can iterate through your array with map() method and inside it extract the value of a first entity of an object in your array and simply get a new array with all values:
const data1 =[{PRODUCT : 'P1'}, {PRODUCT: 'P2'}]
const data2 = [{ITEM: 'I1', QUANTITY: 1}, {ITEM: 'I2', QUANTITY: 2 }]
const transformValuesOfArray = (arrayToTransform) =>
arrayToTransform.map(value => {
const firstObjectValue = Object.values(value)[0]
return firstObjectValue
})
console.log(transformValuesOfArray(data1))
console.log(transformValuesOfArray(data2))

Alternative to Object.fromEntries?

I receive an object like this:
this.data = {
O: {
id: 0,
name: value1,
organization: organization1,
...,
},
1: {
id: 1,
name: value1,
organization: organization1,
...,
},
2: {
id: 2,
name: value2,
organization: organization2,
...,
},
...
}
I then filter by id and remove the Object which id matches the id I receive from the store like so:
filterOutDeleted(ids: any[], data: object,) {
const remainingItems = Object.fromEntries(Object.entries(data)
.filter(([, item]) => !ids.some(id => id === item.id)));
const rows = Object.keys(remainingItems).map((item) => remainingItems[item]);
return rows;
}
Unfortunately, I'm getting an error when building stating Property 'fromEntries' does not exist on type 'ObjectConstructor' and I am unable to make changes in the tsconfig file at this point. Is there an alternative for fromEntries for this case? Any help is much appreciated!
Create the object outside instead, and for every entry that passes the test, assign it to the object manually.
Also note that you can decrease the computational complexity by constructing a Set of the ids in advance:
const filterOutDeleted = (ids: any[], data: object) => {
const idsSet = new Set(ids);
const newObj = {};
for (const [key, val] of Object.entries(data)) {
if (!idsSet.has(val.id)) {
newObj[key] = val;
}
}
return newObj;
};

Assign multiple keys a certain value using ramda.js assocPath?

I am quite new to JS and ramda.js. Let's say i have an object consisting of empty objects like this:
obj = { 1: { }, 2: { }, 3: { } }
and array consisting of chosen keys of the obj .
arr = ['1', '2']
What i need is to create a certain key-value pair, for example a: 'value', inside of the key objects chosen via arr, so the result would look like this:
obj = {
1: { a: 'value' },
2: { },
3: { a: 'value' }
}
I have tried to .map through the keys with
arr.map(key => assocPath([key, 'a'], 'value', obj) )
, and also tried a way with arr.forEach(), but it doesn't work and i believe i may be missing some basic knowledge? Or is there an alternative ramda.js function i should use?
Take a look at my solution
const object = { 1: { }, 2: { }, 3: { } }
const array = ['1', '3']
function magic(obj, arr) {
return arr.reduce((acc, key) => ({
...acc,
[key]: { a: 'value' },
}), obj)
}
console.log(magic(object, array))
This is also can be achieved with ramda's functions
const object = { 1: { }, 2: { }, 3: { } }
const array = ['1', '3']
const magicR = R.reduce((acc, key) => R.assocPath([ key, 'a' ], 'value', acc))
console.log(magicR(object, array))
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
You should be able to do this with .forEach():
arr.forEach(key => obj[key] = { a: 'value' });
The .map() function is for creating a new array from the elements of a source array, and you don't need to do that.
Ramda methods are immutable - they don't mutate the original object, but return a new one. The R.assocPath method is no different in this regard. To update the object, you'll need to iterate the array with R.reduce, use R.assocPath, get a new object, etc...
const { reduce, assocPath } = R
const fn = reduce((o, key) => assocPath([key, 'a'], 'value', o))
const obj = { 1: { }, 2: { }, 3: { } }
const arr = ['1', '2']
const result = fn(obj)(arr)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

track path of recursive function

I'm trying to track path of a deep nested value in json object but having hard time getting the path. Each Item is an array of objects and can have child items. If the object c exists in the json data it is always located in the last item array.
item: [
{
a:5,
item: [
{
item: [
{c:1},
{x:4},
],
...
},
{},
{}
]
},
{},
{}
]
const findPath = (items) => {
let path = []
items.forEach((item,i) => {
if('item' in item){
path = path.concat(findPath(item.item))
}
else if('c' in item) {
path.push(i)
}
})
return path
}
if I have 3 c objects with different item depths, then I would have:
[
[0,0,0], //item[0].item[0].item[0].c
[1,0], //item[1].item[0].c
[4]] , //item[4].c
Any help?
Your main problem here is that you don't track the common case. You store the index only when you found a leaf, but you want all the steps in between. This being recursion, you also have to carry your return values with you, or you end up stepping on them. This works:
objects = [
{},
{
item: [
{},
{},
{
a:5,
item: [
{
item: [
{c:1},
{x:4},
]
},
{},
{}
]
},
{}
]
}
]
const findPath = (items, current_path, matching_paths) => {
items.forEach((item,i) => {
if('item' in item){
current_path.push(i);
current_path = current_path.concat(
findPath(item.item, current_path, matching_paths)
);
}
else if('c' in item) {
current_path.push(i);
matching_paths.push( current_path.slice() );
current_path = [];
}
})
}
var path = [];
var paths = [];
findPath(objects, path, paths);
console.log(paths); //[[1, 2, 0, 0]]
If C is found push a path object to the path array and update that path object for the rest of the paths.
const findPath = (items) => {
let path = []
items.forEach((item,i) => {
if('item' in item){
let item_path = findPath(item.item)
if(item_path.length > 0){
item_path[0].path.push(i)
path.push(item_path[0])
}
}
else if('c' in item){
path.push({path:[i], c:item.c})
}
})
return path
}
The function must be recursive, which means it should call itself with different parameters and not loop forever.
Below is what you are looking for. I made it in TypeScript to make sure I typed it correctly, but just take off all type definitions and it becomes JavaScript:
const trackPath: number[][] = [];
function findPath(topItem: any, path: number[], position: number): void
{
const currentPath = path.slice();
currentPath.push(position);
const newTopItem = topItem['item'];
if (Array.isArray(newTopItem)) {
// here is the recursion for each subitem
newTopItem.forEach((item, i) => findPath(item, currentPath, i));
}
if ('c' in topItem) {
trackPath.push(currentPath);
}
}
// this is the main method to call
function actuallyGetThePath(myTopItem: any): number[][] {
findPath(myTopItem, [], 0);
return trackPath;
}
Good luck!

Categories

Resources