如何在JavaScript中更新对象

时间:2019-06-12 12:47:52

标签: javascript object

我有一个如下的对象结构。

    bookmarks:""
    defaultcompany:"26740"
    defaultmarkets:defaultmarket:Array(6)
    0:{company: "3569", market: "29"}
    1:{company: "26740", market: "25"}
    2:{company: "2677", market: "3"}
    3:{company: "19878", market: "7"}
    4:{company: "1063", market: "1"}
    5:{company: "28215", market: "23"}
    length:6
    spotlightdays:"3"
    userid:"11130286"

如何在javascript中更新对象,以便可以在defaultMarket Array下添加一个或多个数组元素,如下所示。我需要保留原始值并添加一些新值吗?另外,也可以修改现有的数组元素。

    bookmarks:""
    defaultcompany:"26740"
    defaultmarkets:defaultmarket:Array(7)
    0:{company: "3569", market: "29"}
    1:{company: "26740", market: "25"}
    2:{company: "2677", market: "8"}
    3:{company: "19878", market: "7"}
    4:{company: "1063", market: "35"}
    5:{company: "28215", market: "23"}
    6:{company: "67890",market:"45"
    length:7
    spotlightdays:"3"
    userid:"11130286"

2 个答案:

答案 0 :(得分:0)

我相信这两个功能可以满足您的需求:

var obj = {
  bookmarks: "",
  defaultcompany: "26740",
  defaultmarkets: {
    defaultmarket: [
      {company: "3569", market: "29"},
      {company: "26740", market: "25"},
      {company: "2677", market: "3"},
      {company: "19878", market: "7"},
      {company: "1063", market: "1"},
      {company: "28215", market: "23"}
    ]
  },
  length: 6,
  spotlightdays: "3",
  userid: "11130286"
}

// Add an object to the defaultmarket array
const addObject = newObj => obj.defaultmarkets.defaultmarket.push(newObj)

// Edit a market value in the defaultmarket array by the company value
const editMarketByCompany = (company, newValue) => {
  var arr = obj.defaultmarkets.defaultmarket
  arr[arr.findIndex(el => el.company === company)].market = newValue
}

addObject({company: "67890", market: "45"})
editMarketByCompany("2677", "8")

console.log(obj)

答案 1 :(得分:-1)

如果给定对象是[Route("api/v1/camera/live/stream")] [HttpGet] public HttpResponseMessage GetStream() { logger.Trace($"GET api/v1/camera/live/stream called"); var response = Request.CreateResponse(HttpStatusCode.PartialContent); response.Content = new PushStreamContent(new Action<Stream, HttpContent, TransportContext>(async (stream, content, tansportContext) => { EventHandler<AcquiredDataEventArgs<CameraFrame>> handler = (_, __) => { }; try { MjpegWriter writer = new MjpegWriter(stream); handler = (s, e) => writer.Write(e.Data.Image.GetJpegStream()); camera.DataAcquired += handler; while (HttpContext.Current.Response.IsClientConnected) { await Task.Delay(100); } camera.DataAcquired -= handler; } catch (Exception ex) { logger.Error($"Streaming error", ex); throw; } logger.Trace("streaming ended"); })); response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=--boundary"); return response; } ,它应该像obj.defaultmarkets.defaultmarket.push(elmToAdd)一样简单 let obj = { bookmarks:"" defaultcompany:"26740" defaultmarkets:defaultmarket:Array(6) 0:{company: "3569", market: "29"} 1:{company: "26740", market: "25"} 2:{company: "2677", market: "8"} 3:{company: "19878", market: "7"} 4:{company: "1063", market: "35"} 5:{company: "28215", market: "23"} 6:{company: "67890",market:"45" length:6 spotlightdays:"3" userid:"11130286"}