Home > Mobile >  access specific object value and change it using lodash
access specific object value and change it using lodash

Time:03-24

[
{
    "type": "image",
    "version": "3.6.6",
    "originX": "left",
    "originY": "top",
    "left": 93.41,
    "top": 156,
    "width": 100,
    "height": 100,
    "fill": "rgb(0,0,0)",
    "stroke": null,
    "strokeWidth": 0,
    "strokeDashArray": null,
    "strokeLineCap": "butt",
    "strokeDashOffset": 0,
    "strokeLineJoin": "miter",
    "strokeMiterLimit": 4,
    "scaleX": 1,
    "scaleY": 1,
    "angle": 0,
    "flipX": false,
    "flipY": false,
    "opacity": 1,
    "shadow": null,
    "visible": true,
    "clipTo": null,
    "backgroundColor": "transparent",
    "fillRule": "nonzero",
    "paintFirst": "fill",
    "globalCompositeOperation": "source-over",
    "transformMatrix": null,
    "skewX": 0,
    "skewY": 0,
    "crossOrigin": "",
    "cropX": 0,
    "cropY": 0,
    "src": "",
    "filters": []
},
{
    "type": "textbox",
    "version": "3.6.6",
    "originX": "left",
    "originY": "top",
    "left": 127.91,
    "top": 188.41,
    "width": 30,
    "height": 34.17,
    "fill": "rgba(0, 0, 0, 1)",
    "stroke": "rgba(255, 255, 255, 0)",
    "strokeWidth": 1,
    "strokeDashArray": null,
    "strokeLineCap": "butt",
    "strokeDashOffset": 0,
    "strokeLineJoin": "miter",
    "strokeMiterLimit": 4,
    "scaleX": 1,
    "scaleY": 1,
    "angle": 0,
    "flipX": false,
    "flipY": false,
    "opacity": 1,
    "shadow": null,
    "visible": true,
    "clipTo": null,
    "backgroundColor": "",
    "fillRule": "nonzero",
    "paintFirst": "fill",
    "globalCompositeOperation": "source-over",
    "transformMatrix": null,
    "skewX": 0,
    "skewY": 0,
    "text": "new text",
    "fontSize": 14,
    "fontWeight": "normal",
    "fontFamily": "Times New Roman",
    "fontStyle": "normal",
    "lineHeight": 1.16,
    "underline": false,
    "overline": false,
    "linethrough": false,
    "textAlign": "left",
    "textBackgroundColor": "",
    "charSpacing": 0,
    "minWidth": 20,
    "splitByGrapheme": false,
    "styles": {}
}

] I have the following object as klass for lodash I want to access width for every object and change it to the specific value I have tried._map but it didn't work. i also tried to use the map function of javascript (Array.protype.map) but it didn;t work any idea on how can i override the object value in lodash and save it. ?

CodePudding user response:

what exacly would you like to change? which property?

you can use map method and iterate thought each array item and change there any property

const newUpdatedArray = array.map(i => ({...i, type: 'new type', version: Math.random()}))

in my example I changed type and version in each object

const array = [
{
    "type": "image",
    "version": "3.6.6",
    "originX": "left",
    "originY": "top",
    "left": 93.41,
    "top": 156,
    "width": 100,
    "height": 100,
    "fill": "rgb(0,0,0)",
    "stroke": null,
    "strokeWidth": 0,
    "strokeDashArray": null,
    "strokeLineCap": "butt",
    "strokeDashOffset": 0,
    "strokeLineJoin": "miter",
    "strokeMiterLimit": 4,
    "scaleX": 1,
    "scaleY": 1,
    "angle": 0,
    "flipX": false,
    "flipY": false,
    "opacity": 1,
    "shadow": null,
    "visible": true,
    "clipTo": null,
    "backgroundColor": "transparent",
    "fillRule": "nonzero",
    "paintFirst": "fill",
    "globalCompositeOperation": "source-over",
    "transformMatrix": null,
    "skewX": 0,
    "skewY": 0,
    "crossOrigin": "",
    "cropX": 0,
    "cropY": 0,
    "src": "",
    "filters": []
},
{
    "type": "textbox",
    "version": "3.6.6",
    "originX": "left",
    "originY": "top",
    "left": 127.91,
    "top": 188.41,
    "width": 30,
    "height": 34.17,
    "fill": "rgba(0, 0, 0, 1)",
    "stroke": "rgba(255, 255, 255, 0)",
    "strokeWidth": 1,
    "strokeDashArray": null,
    "strokeLineCap": "butt",
    "strokeDashOffset": 0,
    "strokeLineJoin": "miter",
    "strokeMiterLimit": 4,
    "scaleX": 1,
    "scaleY": 1,
    "angle": 0,
    "flipX": false,
    "flipY": false,
    "opacity": 1,
    "shadow": null,
    "visible": true,
    "clipTo": null,
    "backgroundColor": "",
    "fillRule": "nonzero",
    "paintFirst": "fill",
    "globalCompositeOperation": "source-over",
    "transformMatrix": null,
    "skewX": 0,
    "skewY": 0,
    "text": "new text",
    "fontSize": 14,
    "fontWeight": "normal",
    "fontFamily": "Times New Roman",
    "fontStyle": "normal",
    "lineHeight": 1.16,
    "underline": false,
    "overline": false,
    "linethrough": false,
    "textAlign": "left",
    "textBackgroundColor": "",
    "charSpacing": 0,
    "minWidth": 20,
    "splitByGrapheme": false,
    "styles": {}
}]


const newUpdatedArray = array.map(i => ({...i, type: 'new type', version: Math.random()}))

console.log(newUpdatedArray)

  • Related