I want to change the value of each objects inside the array. I want to do a path.parse(name).name on the name object inside of the item array and return this array with the new values
My array is like this :
[
{
"name": "core Test",
"item": [
{
"name": "test/core/core.js",
"item": []
}
]
},
{
"name": "users Test",
"item": [
{
"name": "test/users/user.js",
"item": []
}
]
}
]
here is my code :
array.forEach((element) => {
const { item } = element;
item.forEach((i) => {
const { name } = i;
const newname = path.parse(name).name;
console.log(newname);
});
});
The output i want is :
[
{
"name": "core Test",
"item": [
{
"name": "core",
"item": []
}
]
},
{
"name": "users Test",
"item": [
{
"name": "user",
"item": []
}
]
}
]
This will give you your desired output
const path = require('path')
const datas = [
{
name: 'core Test',
item: [
{
name: 'test/core/core.js',
item: []
}
]
},
{
name: 'users Test',
item: [
{
name: 'test/users/user.js',
item: []
}
]
}
]
const transformedData = datas.map(data => {
data.item = data.item.map(x => ({
name: path.parse(x.name).name,
item: x.item
}))
return data
})
console.log(transformedData )
Related
I have this JSON data:
{
"data": [
{
"category": {
"documentId": "c8kr0cv012vtr8vm3iug",
"title": "Art"
},
"subcategories": [
{
"documentId": "c8kr7nv012vtr8vm3l8g",
"title": "Architecture"
},
{
"documentId": "c8kr7nv012vtr8vm3lag",
"title": "Dance"
},
{
"documentId": "c8kr7nv012vtr8vm3lbg",
"title": "Fashion"
}
]
},
{
"category": {
"documentId": "c8kr0cv012vtr8vm3iqg",
"title": "Business"
},
"subcategories": [
{
"documentId": "c8kr3d7012vtr8vm3jjg",
"title": "Crypto"
},
{
"documentId": "c8kr3d7012vtr8vm3jj0",
"title": "Finance"
},
{
"documentId": "c8kr3d7012vtr8vm3jkg",
"title": "Marketing"
}
]
}
]
}
I have tagview in my mobile screen so user can select multiple subcategory at a same time by select deselect so if i choose 2 subcategory from Art and 2 from Business then expected post payload should be like below
{
"data": [
{
"category": "c8kr0cv012vtr8vm3iug",
"subcategories": [
"c8kr7nv012vtr8vm3l8g",
"c8kr7nv012vtr8vm3lag"
]
},
{
"category": "c8kr0cv012vtr8vm3iqg",
"subcategories": [
"c8kr3d7012vtr8vm3jjg",
"c8kr3d7012vtr8vm3jj0"
]
}
]
}
So my code is like below
const tempDefaultPayload = dataPayload.map(x => {
if (x.category) {
x.subcategories?.push(subcategory);
if (userData?.userInterests?.map(v => v.category.documentId === category)) {
const arrayNewData: any = [];
const newData: any = userData?.userInterests?.map(i =>
i.subcategories?.map(k => {
arrayNewData.push(k?.documentId);
return k?.documentId;
}),
);
x.subcategories?.push(...arrayNewData);
}
return x;
}
return x;
})
When I run above code payload is not passing correct
[
{
"category": "c8kr0cv012vtr8vm3iug",
"subcategories": [
"c8kr7nv012vtr8vm3l8g",
"c8kr7nv012vtr8vm3lag",
"c8kr3d7012vtr8vm3jjg",
"c8kr3d7012vtr8vm3jj0"
]
},
{
"category": "c8kr0cv012vtr8vm3iqg",
"subcategories": [
"c8kr7nv012vtr8vm3l8g",
"c8kr7nv012vtr8vm3lag",
"c8kr3d7012vtr8vm3jjg",
"c8kr3d7012vtr8vm3jj0"
]
}
]
It is passing all selected subcategory in both array any idea how can I solve this ?
If you have the selected tag ids in the array selectedTags and your json as a variable called data:
const tempDefaultPayload = {
data: data.data.map(x => {
"category": x.category.documentId,
"subcategories": x.subcategories.map(subcat =>
subcat.documentId).filter(item => selectedTags.includes(item))
})
}
Playground example
I am trying to convert object with object inside to array of objects.
I have the data as:
"data" :{
"tDetails": {
"tName": "Limited",
"tPay": " xyz",
},
"bDetails": {
"bName": "name",
"bid": "bid",
"bNo": "123456",
},
"iDetails": {
"iName": "iname",
"iBranch": "ibranch",
},
}
i want to convert it to array of objects with each iteration over the three headers(tDetails, iDetails,bDetails) as:
const newData = [
{
"id": 1,
"title": "tDetails",
"content": [
{
"id": 1,
"key": "tName",
"value": "Limited"
},
{
"id": 2,
"key": "tPay",
"value": "xyz"
}
]
},
{
"id": 2,
"title": "bDetails",
"content": [
{
"id": 1,
"key": "bId",
"value": "12345"
}
]
},
{
"id": 3,
"title": "iDetails",
"content": [
{
"id": 1,
"key": "iName",
"value": "iname"
},{
"id":2,
"key": "iBranch",
"value": "ibranch"
}
]
},
]
const NewData = () => {
let newDetails = [];
let newHeader = '';
for (const header in data) {
// header here is 'tDetails', bDetails, iDetails
const headerData = data[header];
newDetails = Object.entries(headerData).map(([key, value]) => ({
key,
value,
}));
newHeader = header;
}
return [
{
title: newHeader,
content: newDetails,
},
];
};
This returns me the data of only the last part(iDetails) as it is returned after the for loop.What should be changed here
Map the object's entries instead of using for..in, and in the callback, return the transformed object.
const data={tDetails:{tName:"Limited",tPay:" xyz"},bDetails:{bName:"name",bid:"bid",bNo:"123456"},iDetails:{iName:"iname",iBranch:"ibranch"}};
const newData = Object.entries(data).map(([title, obj], i) => ({
title,
id: i + 1,
content: Object.entries(obj).map(([key, value], j) => ({
id: j + 1,
key,
value
}))
}));
console.log(newData);
I have this object below
{
"root": {
"data": {
"page": 1,
"contents": [
{
"node": {
"id": "UzpfSTE",
"stats": {
"viewers": {
"nodes": [
{
"id": "1",
"name": "John"
},
{
"id": "2",
"name": "Shaun"
}
]
}
}
}
},
{
"node": {
"id": "UzpfSTG",
"stats": {
"viewers": {
"nodes": [
{
"id": "3",
"name": "Liam"
}
]
}
}
}
}
]
}
}
}
There is contents node, each of them will have many viewers, all I want is to extract all viewers name to an array, in this instance my result will be [John, Shaun, Liam]
I have this approach:
const data = JSON.parse(rt)
const contents = data.root.data.contents
const newArray = []
for (i = 0; i < contents.length; i++) {
arr2 = contents[i].node.stats.viewers.nodes
for (n = 0; n < arr2.length; n++) {
name = arr2[n].name
newArray.push(name)
}
}
console.log(newArray)
>>> [John, Shaun, Liam]
Which does the job, but occasionaly the object key names change and I have to alter everytime.
So is there more elegant way to do this?
You can simplify that imperative logic like this.
I don't understand what you mean by "the object key names change" though
const data = {
"root": {
"data": {
"page": 1,
"contents": [{
"node": {
"id": "UzpfSTE",
"stats": {
"viewers": {
"nodes": [{
"id": "1",
"name": "John"
},
{
"id": "2",
"name": "Shaun"
}
]
}
}
}
},
{
"node": {
"id": "UzpfSTG",
"stats": {
"viewers": {
"nodes": [{
"id": "3",
"name": "Liam"
}]
}
}
}
}
]
}
}
}
const names = data.root.data.contents.flatMap(({
node: {
stats: {
viewers: {
nodes
}
}
}
}) => nodes.map(({
name
}) => name))
console.log(names)
const data = JSON.parse(rt)
const contents = data.root.data.contents
const viewers = contents.map(item => item.node.stats.viewers.nodes).flat()
const viewerNames = viewers.map(viewer => viewer.name)
I would like to obtain the value of name using the code below.
When find(z => z.key.value === 123) be added to the code ,there will be this error :
x.obj.array_1.find(...).find is not a function
What's wrong with this code :
const data = [{
"obj": {
"array_1": [{
"array_2": [{
"array_3": [{
"key": {
"value": "123"
}
}]
}]
}]
},
"name": "obj1"
},
{
"obj": {
"array_1": [{
"array_2": [{
"array_3": [{
"key": {
"value": "456"
}
}]
}]
}]
},
"name": "obj2"
},
{
"obj": {
"array_1": [{
"array_2": [{
"array_3": [{
"key": {
"value": "789"
}
}]
}]
}]
},
"name": "obj3"
}
]
const name = data
.filter(x =>
x.obj.array_1.find(y => y.array_2).find(y => y.array_3).find(z => z.key.value === 123)
).map(x => x.name);
console.log(name)
Small fix here, this work provided that your real life data is also in this shape
After finding the propriate array_1 element that have array2[0] which has non-nil array_3, you have to access that array_3 to iterate and find the expected value
Edited (because array_2 and array_3 may have multiple elements): you have to extract all array_3 element (using flatMap to flatten the nested array) and do the condition check from that extracted array
const name = data
.filter((x) => {
const flattened_array_3 = x.obj.array_1.flatMap((array_1_el) =>
array_1_el.array_2.flatMap((array_2_el) => array_2_el.array_3)
)
return flattened_array_3.find((array_3_el) => +array_3_el.key.value === 123)
})
.map((x) => x.name)
Full code
const data = [
{
obj: {
array_1: [
{
array_2: [
{
array_3: [
{
key: {
value: "123",
},
},
],
},
],
},
],
},
name: "obj1",
},
{
obj: {
array_1: [
{
array_2: [
{
array_3: [
{
key: {
value: "456",
},
},
],
},
],
},
],
},
name: "obj2",
},
{
obj: {
array_1: [
{
array_2: [
{
array_3: [
{
key: {
value: "789",
},
},
],
},
],
},
],
},
name: "obj3",
},
]
const name = data
.filter((x) => {
const flattened_array_3 = x.obj.array_1.flatMap((array_1_el) =>
array_1_el.array_2.flatMap((array_2_el) => array_2_el.array_3)
)
return flattened_array_3.find((array_3_el) => +array_3_el.key.value === 123)
})
.map((x) => x.name)
console.log(name)
Reference: flatMap
x.obj.array_1.find(y => y.array_2)
is undefined.
Try to split your code
In order to work on this structure, I advise you to change the way you represent data.
It seems like some kind of output of tokenization. There have to bee tools to extract information from the path for example XPath, etc.
On the other hand below code should work for your case
The key point is using flatMap to rest inner arrays to upper one
const data = [{
"obj": {
"array_1": [{
"array_2": [{
"array_3": [{
"key": {
"value": "123"
}
}]
}]
}]
},
"name": "obj1"
},
{
"obj": {
"array_1": [{
"array_2": [{
"array_3": [{
"key": {
"value": "456"
}
}]
}]
}]
},
"name": "obj2"
},
{
"obj": {
"array_1": [{
"array_2": [{
"array_3": [{
"key": {
"value": "789"
}
}]
}]
}]
},
"name": "obj3"
}
]
var obj = data.find(x=> x.obj.array_1.flatMap(x=>x.array_2).flatMap(x=>x.array_3)[0].key.value==789)
console.log(obj.name)
I have two arrays, one is my original one called data which consists of :
const datas = [
{
name: 'core Test',
item: [
{
name: 'test/core/core.js',
item: "item1"
}
]
},
{
name: 'users Test',
item: [
{
name: 'test/users/user.js',
item: "item2"
}
]
}
]
And i have another array called replace, which i'm trying to push each of its elements inside my original one, inside the
const replace = [
{
type: "test1",
number: "1",
},
{
type: "test2",
number: "2",
}
]
Here is my code :
const transformedData = datas.map(data => {
data.item = data.item.map(x => ({
name: x.name,
type: replace.map(y=>{return y;})
}))
return data
})
The output i get :
[
{
"name": "core Test",
"item": [
{
"name": "test/core/core.js",
"type": [
{ "type": "test1", "number": "1" },
{ "type": "test2", "number": "2" }
]
}
]
},
{
"name": "users Test",
"item": [
{
"name": "test/users/user.js",
"type": [
{ "type": "test1", "number": "1" },
{ "type": "test2", "number": "2" }
]
}
]
}
]
The output i want :
[
{
"name": "core Test",
"item": [
{
"name": "test/core/core.js",
"type": { "type": "test1", "number": "1" }
}
]
},
{
"name": "users Test",
"item": [
{
"name": "test/users/user.js",
"type": { "type": "test2", "number": "2" }
}
]
}
]
This is because you're mapping all the way through the replace array every single time for each time you're inside of a value inside of datas. Instead you want to keep track of the index with your original map so then you only have one instance each time.
Try something like:
const transformedData = datas.map((data, index) => {
data.item = data.item.map(x => ({
name: x.name,
type: replace[index]
}))
return data;
});