小编Yvo*_*onC的帖子

React: "Error t.filter is not a function" 或 "Error: Uncaught TypeError: t.find is not a function" --> 尝试更新数组中的对象

React 新手把事情搞砸了,抱歉,但确实尝试了几个小时;(请参阅下面的尝试。

简单任务:尝试更新对象数组中的对象。

这应该相当容易,尽管在研究了十几个答案之后,尝试了一堆可能的解决方案,但我仍然遇到错误。我不知道我在这里缺少什么。

这是我的4次尝试:

尝试1

updateText = (updatedText) => {

  var arrTexts = {...this.state.arrTexts}

  var myObjectToUpdate = arrTexts.filter(x => x.id === updatedText.id);
  myObjectToUpdate = updatedText;

  console.log (myObjectToUpdate);
  console.log (arrTexts);
};
Run Code Online (Sandbox Code Playgroud)

尝试2:

updateText = (updatedText) => {

  var arrTexts = {...this.state.arrTexts}

  var myObjectToUpdate = arrTexts.find(function (myObjectToUpdate) { return myObjectToUpdate.id === updatedText.id; });
  myObjectToUpdate = updatedText

  console.log (myObjectToUpdate);
  console.log (arrTexts);
};
Run Code Online (Sandbox Code Playgroud)

尝试3

updateText = (updatedText) => {

  var arrTexts = {...this.state.arrTexts}

  var myObjectToUpdate = arrTexts.findIndex(x => x.id === updatedText.id);
  myObjectToUpdate = …
Run Code Online (Sandbox Code Playgroud)

javascript object filter find reactjs

4
推荐指数
1
解决办法
2万
查看次数

嵌套模型表单 - Railscast#196修订 - 通过jQuery添加字段不起作用

我正在遵循Railscast#196修订版,一切都基本上没问题,但我无法在嵌套表单中添加新字段."删除字段"功能运行良好,但点击"link_to_add_fields"后,浏览器会跳转到页面顶部,并且不会显示新字段.

这个轨道广播已经有很多问题了,比如这里这里,我试着阅读所有这些问题,但大多数都是指2010年的原始演员.我现在被困了几个小时,我不能弄清楚,我的问题在哪里.对不起,如果它是一个新手的错误,我对rails很新.任何帮助将非常感激!

我遵循#196修订版,使用Rails 3.2.13,Ruby 1.9.3

车型/ post.rb

class Post < ActiveRecord::Base
    attr_accessible :content, :title, :image, :postfiles_attributes
    has_many :postfiles, :dependent => :destroy 
    accepts_nested_attributes_for :postfiles, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)

车型/ postfile.rb

class Postfile < ActiveRecord::Base
  attr_accessible :dropbox, :gdrive, :post_id
  belongs_to :post
end
Run Code Online (Sandbox Code Playgroud)

意见/职位/ _form.html.erb

<%= simple_form_for @post do |f| %>

  <%= f.fields_for :postfiles do |builder| %>
       <%= render 'postfile_fields', f: builder %>
  <% end %>
  <%= link_to_add_fields "Add File", f, :postfiles %>

<% end %>
Run Code Online (Sandbox Code Playgroud)

意见/职位/ _postfile_fields.html.erb …

jquery ruby-on-rails nested-forms railscasts ruby-on-rails-3.2

3
推荐指数
1
解决办法
1588
查看次数

JS-过滤对象数组以查找一个属性的重复项,并根据另一个属性确定要保留的对象

尝试通过消除具有另一个对象中已经存在的特定属性的对象来过滤对象数组(重复项)。决定删除哪个对象应该基于另一个属性。

示例:对于一组看起来像这样的对象,目标是过滤所有“用户”重复项,并保留具有最旧“日期”属性的对象。

const arr = [
    {user: 'Alex', date: '1540801929945'},
    {user: 'Bill', date: '1640801929946'},
    {user: 'Carl', date: '1740801929947'},
    {user: 'Alex', date: '1840801929948'},
]
Run Code Online (Sandbox Code Playgroud)

预期结果:

filteredArr = [
    {user: 'Alex', date: '1540801929945'},
    {user: 'Bill', date: '1640801929946'},
    {user: 'Carl', date: '1740801929947'},
]
Run Code Online (Sandbox Code Playgroud)

我管理了仅用于保留唯一对象的基本过滤。

const filteredArr = arr.reduce((unique, o) => {
    if(!unique.some(obj => obj.user === o.user) {
      unique.push(o);
    }
    return unique;
},[]);
Run Code Online (Sandbox Code Playgroud)

尽管我不知道,该怎么做,以便在重复的情况下保留“最旧的”对象,并删除最新的对象。非常感谢你的帮助!非常感谢。

javascript arrays filter duplicates javascript-objects

3
推荐指数
1
解决办法
1331
查看次数