访问特定的对象值并使用 lodash 更改它

access specific object value and change it using lodash

[
{
    "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": {}
}

] 我有以下对象作为 lodash 的 klass 我想访问每个对象的宽度并将其更改为我拥有的特定值 tried._map 但它没有用。我还尝试使用 javascript (Array.protype.map) 的映射函数,但它没有;我不知道如何覆盖 lodash 中的对象值并保存它。 ?

您想具体改变什么? 属性?

您可以使用 map 方法并迭代思想每个数组项并在那里更改任何 属性

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

在我的示例中,我更改了每个对象中的 typeversion

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)